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

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?


Solution 1:

I've found three ways to do this:

  1. The simple way:

    sudo apt-get install python3-gi

    or

  2. For virtualenv users - The vext way

    pip install vext

    pip install vext.gi

  3. The pure python developer way:

    Install a bunch of developer stuff:

    sudo apt-get 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