What would cause the "gi" module to be missing from Python?

172,774

Solution 1

I've found three ways to do this:

  1. The simple way:

    sudo apt install python3-gi
    
  2. The vext way, for virtualenv users:

    pip install vext
    pip install vext.gi
    
  3. 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.

Share:
172,774

Related videos on Youtube

Colin Dumitru
Author by

Colin Dumitru

Updated on September 18, 2022

Comments

  • Colin Dumitru
    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
      Javier Rivera over 12 years
      Have you tried to reinstall python-gobjects ?
    • Colin Dumitru
      Colin Dumitru over 12 years
      I did, but it re-installed "python-gobject 3.0.0-0ubuntu4". Is this the correct version number for 2.7?
    • sagarchalise
      sagarchalise over 12 years
      have you tried reconfiguring python i.e. sudo dpkg-reconfigure python2.7 ?
    • Colin Dumitru
      Colin Dumitru over 12 years
      Yes, but the same error persists.
    • Aris
      Aris over 4 years
      for 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
    Matthew about 6 years
    In this case the packages are shared - so works for both. Have removed the reference to Debian to avoid confusion.
  • ArchLinuxTux
    ArchLinuxTux almost 6 years
    This solved the problem for me on Arch Linux! The other solution by Jeremy Bicha, installing python-gobject on Arch Linux (or python3-gi on Ubuntu) did not work for me!
  • matanster
    matanster about 4 years
    Only way that worked for me this time around
  • Doğuş
    Doğuş almost 4 years
    Caution: 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
    Cerin over 3 years
    Installing 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)