What would cause the "gi" module to be missing from Python?
Solution 1
I've found three ways to do this:
-
The simple way:
sudo apt install python3-gi
-
The vext way, for virtualenv users:
pip install vext pip install vext.gi
-
The pure Python developer way:
Install a bunch of developer stuff:
sudo apt install pkg-config libcairo2-dev gcc python3-dev libgirepository1.0-dev
Install the python packages:
pip install gobject PyGObject
Solution 2
If you installed anaconda and it has become the default python environment, gi module can be installed via:
conda install -c conda-forge pygobject
Also see: https://bugs.launchpad.net/ubuntu/+source/ibus/+bug/1611523
Solution 3
Little workaround found here that worked for me:
sudo nano /usr/bin/gnome-tweak-tool
Change #!/usr/bin/env python
to #!/usr/bin/python
Solution 4
Run this command in the terminal:
sudo apt install python3-gi
or if you need Python2 support instead:
sudo apt install python-gi
Solution 5
sudo apt-get install -y python-gobject
Fixed it for me on Ubuntu 18.04.
Related videos on Youtube
Colin Dumitru
Updated on September 18, 2022Comments
-
Colin Dumitru almost 2 years
After some not so clever editing of the default Python version in Ubuntu, from 2.7 to 3.2, I ended up breaking my entire system. After my computer imploded and everything stopped working, I tried to revert back my changes (by linking
/usr/bin/python2.7
to/usr/bin/python
and changing the default version in/usr/share/python/debian_defaults
back to 2.7) but some things are still broken.For example, when I type
import gi
in the Python interpreter I get the following message:>>> import gi Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named gi >>>
error which appears with some programs too (eg: gnome tweak tool). I have tried re-installing Python both from the software center and from sources, but the same error persists.
python --version
now returns:Python 2.7.2
and also some software packages which depend on Python 2.7 are now working (for example the software center), but some things are still broken. Is there anything I can do to completely re-install Python 2.7 as the default version?
-
Javier Rivera over 12 yearsHave you tried to reinstall python-gobjects ?
-
Colin Dumitru over 12 yearsI did, but it re-installed "python-gobject 3.0.0-0ubuntu4". Is this the correct version number for 2.7?
-
sagarchalise over 12 yearshave you tried reconfiguring python i.e.
sudo dpkg-reconfigure python2.7
? -
Colin Dumitru over 12 yearsYes, but the same error persists.
-
Aris over 4 yearsfor those have updated python to 3.7 manually, all you have to do is to edit /usr/bin/gnome-tweaks, and: change the #!/usr/bin/python3 to #!/usr/bin/python3.6. Everything should work like a charm after this
-
-
Matthew about 6 yearsIn this case the packages are shared - so works for both. Have removed the reference to Debian to avoid confusion.
-
ArchLinuxTux almost 6 yearsThis solved the problem for me on Arch Linux! The other solution by Jeremy Bicha, installing
python-gobject
on Arch Linux (orpython3-gi
on Ubuntu) did not work for me! -
matanster about 4 yearsOnly way that worked for me this time around
-
Doğuş almost 4 yearsCaution: if you do install "python3-dev", it may upgrade your already installed python3, and might create complications for you, as it happened to me previously.
-
Cerin over 3 yearsInstalling the vext packages in my virtualenv only changed the error to
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-packages/gi/__init__.py)