ModuleNotFoundError: No module named 'gi'
Asked Answered
E

5

13

I am trying to run the following program:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst

Bu I am getting:

Traceback (most recent call last):
  File "application.py", line 2, in <module>
    import gi
ModuleNotFoundError: No module named 'gi'

I am on a Mac Sierra (10.12.1 (16B2555)) using Python3.

I've installed pygoject and gtk+3 (as can be seen here):

(cv) ✔ ~/Documents/test 
00:53 $ brew install pygobject3 --with-python3 gtk+3
Warning: pygobject3 3.24.1_1 is already installed
Warning: gtk+3 3.22.18 is already installed

My Python is from my virtualenv:

(cv) ✘-1 ~/Documents/test 
00:53 $ which python
/Users/myuser/.virtualenvs/cv/bin/python

(cv) ✔ ~/Documents/test 
00:54 $ python --version
Python 3.6.1

If I try to uninstall gi to reinstall it (as I saw in some previous answers from Stack) I get:

(cv) ✘-1 ~/Documents/test 
00:55 $ pip uninstall gi
Cannot uninstall requirement gi, not installed

Any idea of what could solve my issue?

Eon answered 14/8, 2017 at 5:10 Comment(5)
My first guess is that you're using your virtualenv python but gi is installed in your system directory. Can you deactivate your virtualenv, try starting python and importing gi? If that works, we can figure something out.Mezereum
Try re-recreating the environment, as the bindings are installed against the system Python - so they may not be reflected in your (old) environment. Do a pip freeze > requirements.txt to capture what you have already installed, then recreate the environment again, and try.Extractive
@NoufalIbrahim apparently this is the case because: 10:18 $ python Python 2.7.10 (default, Jul 30 2016, 18:31:42) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import gi >>> gi.require_version('Gst', '1.0')Eon
@NoufalIbrahim any ideas how to proceed?Eon
@NoufalIbrahim thanks :)Eon
E
8

In my case, as pointed by @NoufalIbrahim, the package was installed in the system directory, but not in the virtualenv.

I have solved the issue by executing:

export PYTHONPATH=/usr/local/lib/python3.6/site-packages
Eon answered 14/8, 2017 at 14:58 Comment(3)
My apologies. I couldn't follow up after my original comment. If this is indeed the problem, you should be able to do a pip install gi inside your virtualenv python and it should bring it in. The other thing you can do is to create your virtualenv with the --system-site-packages option so that it picks up packages in the path you've specified as well. No need to tinker with pythonpath manually.Mezereum
@NoufalIbrahim When I did what you suggested using pip, I got ERROR: Could not find a version that satisfies the requirement gi (from versions: none) ERROR: No matching distribution found for gi. Are you sure gi is the right name of the thingy to install?Mycosis
Are you on a mac?Eon
L
5

Personally I solved running

pip install vext
pip install vext.gi

Another solution I found

pip install homekit
pip install pygobject

(Debian users: you can replace homekit with sudo apt install libgirepository1.0-dev)

Lousewort answered 24/8, 2021 at 11:51 Comment(0)
O
2

You are getting this error due to missing gi library. I solved this by running following command on OSX (Mac):

brew install pygobject3 gtk+3
Ogg answered 12/7, 2019 at 17:32 Comment(1)
That's was not the case for me, if you look at my question, I show that I had run this command, I also show that whenever I tried to run the command again it would say that this library was already installed... the issue was an export site-packages missing....Eon
D
1

I solved it by installing the dependencies recommended in the install instructions of PyGObject:

https://pygobject.readthedocs.io/en/latest/getting_started.html#getting-started

Dollar answered 11/3, 2022 at 18:36 Comment(0)
W
-8

Try running this command:

sudo apt install python3-gi
Woodhead answered 14/8, 2017 at 5:12 Comment(2)
I am on a Mac, and the equivalent command (brew install python3-gi) doesn't work...Eon
Even though this is the most downvoted answer, this SOLVED my side of the problem that even the most upvoted here can't solve. I'm probably just one of those edge-cases you always see in coding competitions, +1.Varices

© 2022 - 2024 — McMap. All rights reserved.