50

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?

7
  • Have you tried to reinstall python-gobjects ? Nov 18, 2011 at 11:28
  • I did, but it re-installed "python-gobject 3.0.0-0ubuntu4". Is this the correct version number for 2.7? Nov 18, 2011 at 11:34
  • Yes. It's the one installed on my system and it's working right. Nov 18, 2011 at 11:37
  • have you tried reconfiguring python i.e. sudo dpkg-reconfigure python2.7 ? Nov 18, 2011 at 11:40
  • Yes, but the same error persists. Nov 18, 2011 at 11:43

12 Answers 12

64

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 package:

    pip install PyGObject
    
3
  • In this case the packages are shared - so works for both. Have removed the reference to Debian to avoid confusion.
    – Matthew
    Jun 10, 2018 at 6:07
  • 1
    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.
    – Doğuş
    Jul 1, 2020 at 10:42
  • 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)
    – Cerin
    Mar 19, 2021 at 17:53
18

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

3
  • 4
    Only way that worked for me this time around
    – matanox
    Jun 15, 2020 at 17:26
  • 1
    This made my day Dec 3, 2022 at 16:37
  • Works like charm!
    – Apogentus
    Mar 11 at 15:26
9

Little workaround found here that worked for me:

sudo nano /usr/bin/gnome-tweak-tool

Change #!/usr/bin/env python to #!/usr/bin/python

1
  • 1
    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! Sep 16, 2018 at 11:06
8

Run this command in the terminal:

sudo apt install python3-gi

or if you need Python2 support instead:

sudo apt install python-gi
3

In my case:

>>> import gi
>>> gi.__file__
'/usr/lib/python2.7/dist-packages/gi/__init__.pyc'

So what you have to check if that you have that file available. If isn't then check that /usr/lib/python2.7/dist-packages/gi/__init__.py points /usr/share/pyshared/gi/__init__.py. You should have that file since you have python-gobject 3.0.0-0ubuntu4 (yes, I have the very same version).

If the gi package it's there, then you should have a look at your sys.path output. If /usr/lib/python2.7/dist-packages isn't included, then add it and try again.

Once the import works, you can just set your PYTHONPATH or investigate further on the reason why the path isn't correctly set.

The pending problem probably is either that your site.py file isn't valid for whatever reason or, more probably, that you're not getting the right site.py (it should be under /usr/lib/python2.7/). In that case have a look here to review all the directories python uses to look for that file and check that your environment is correct.

3
sudo apt-get install -y python-gobject

Fixed it for me on Ubuntu 18.04.

2

Increasing the verbosity of the interpreter goes a long way towards triaging this issue. Even after all that, I tried doing a simple symlink of the 2.7 dist-package to my usr/local 3.x dist-package and got a little further, but not by much.

 sudo ln -s /usr/lib/python2.7/dist-packages/gi/ /usr/local/lib/python3.2/dist-packages

~$ python3
Python 3.2.2+ (default, Feb 16 2012, 19:05:44) 
[GCC 4.6.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.2/dist-packages/gi/__init__.py", line 23, in 
    from ._gi import _API, Repository
ImportError: /usr/lib/libpyglib-gi-2.0-python2.7.so.0: undefined symbol: _Py_ZeroStruct

If you can track down that missing symbol you might be able to solve this problem. Here's all the verbose import output that led me this far. Also, this appears relevant: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=645963 , Good luck.

>>> import gi
# trying gi.cpython-32mu.so
# trying gimodule.cpython-32mu.so
# trying gi.abi3.so
# trying gimodule.abi3.so
# trying gi.so
# trying gimodule.so
# trying gi.py
# trying gi.pyc
# trying /usr/lib/python3.2/gi.cpython-32mu.so
# trying /usr/lib/python3.2/gimodule.cpython-32mu.so
# trying /usr/lib/python3.2/gi.abi3.so
# trying /usr/lib/python3.2/gimodule.abi3.so
# trying /usr/lib/python3.2/gi.so
# trying /usr/lib/python3.2/gimodule.so
# trying /usr/lib/python3.2/gi.py
# trying /usr/lib/python3.2/gi.pyc
# trying /usr/lib/python3.2/plat-linux2/gi.cpython-32mu.so
# trying /usr/lib/python3.2/plat-linux2/gimodule.cpython-32mu.so
# trying /usr/lib/python3.2/plat-linux2/gi.abi3.so
# trying /usr/lib/python3.2/plat-linux2/gimodule.abi3.so
# trying /usr/lib/python3.2/plat-linux2/gi.so
# trying /usr/lib/python3.2/plat-linux2/gimodule.so
# trying /usr/lib/python3.2/plat-linux2/gi.py
# trying /usr/lib/python3.2/plat-linux2/gi.pyc
# trying /usr/lib/python3.2/lib-dynload/gi.cpython-32mu.so
# trying /usr/lib/python3.2/lib-dynload/gimodule.cpython-32mu.so
# trying /usr/lib/python3.2/lib-dynload/gi.abi3.so
# trying /usr/lib/python3.2/lib-dynload/gimodule.abi3.so
# trying /usr/lib/python3.2/lib-dynload/gi.so
# trying /usr/lib/python3.2/lib-dynload/gimodule.so
# trying /usr/lib/python3.2/lib-dynload/gi.py
# trying /usr/lib/python3.2/lib-dynload/gi.pyc
# trying /usr/local/lib/python3.2/dist-packages/gi.cpython-32mu.so
# trying /usr/local/lib/python3.2/dist-packages/gimodule.cpython-32mu.so
# trying /usr/local/lib/python3.2/dist-packages/gi.abi3.so
# trying /usr/local/lib/python3.2/dist-packages/gimodule.abi3.so
# trying /usr/local/lib/python3.2/dist-packages/gi.so
# trying /usr/local/lib/python3.2/dist-packages/gimodule.so
# trying /usr/local/lib/python3.2/dist-packages/gi.py
# trying /usr/local/lib/python3.2/dist-packages/gi.pyc
# trying /usr/lib/python3/dist-packages/gi.cpython-32mu.so
# trying /usr/lib/python3/dist-packages/gimodule.cpython-32mu.so
# trying /usr/lib/python3/dist-packages/gi.abi3.so
# trying /usr/lib/python3/dist-packages/gimodule.abi3.so
# trying /usr/lib/python3/dist-packages/gi.so
# trying /usr/lib/python3/dist-packages/gimodule.so
# trying /usr/lib/python3/dist-packages/gi.py
# trying /usr/lib/python3/dist-packages/gi.pyc
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named gi

The search path doesn't even look at the python2.7 path. Here's how it looks from the successful side of python2.7.

>>> import gi
# trying gi.so
# trying gimodule.so
# trying gi.py
# trying gi.pyc
# trying /usr/lib/python2.7/gi.so
# trying /usr/lib/python2.7/gimodule.so
# trying /usr/lib/python2.7/gi.py
# trying /usr/lib/python2.7/gi.pyc
# trying /usr/lib/python2.7/plat-linux2/gi.so
# trying /usr/lib/python2.7/plat-linux2/gimodule.so
# trying /usr/lib/python2.7/plat-linux2/gi.py
# trying /usr/lib/python2.7/plat-linux2/gi.pyc
# trying /usr/lib/python2.7/lib-tk/gi.so
# trying /usr/lib/python2.7/lib-tk/gimodule.so
# trying /usr/lib/python2.7/lib-tk/gi.py
# trying /usr/lib/python2.7/lib-tk/gi.pyc
# trying /usr/lib/python2.7/lib-dynload/gi.so
# trying /usr/lib/python2.7/lib-dynload/gimodule.so
# trying /usr/lib/python2.7/lib-dynload/gi.py
# trying /usr/lib/python2.7/lib-dynload/gi.pyc
# trying /usr/local/lib/python2.7/dist-packages/gi.so
# trying /usr/local/lib/python2.7/dist-packages/gimodule.so
# trying /usr/local/lib/python2.7/dist-packages/gi.py
# trying /usr/local/lib/python2.7/dist-packages/gi.pyc
import gi # directory /usr/lib/python2.7/dist-packages/gi
# trying /usr/lib/python2.7/dist-packages/gi/__init__.so
# trying /usr/lib/python2.7/dist-packages/gi/__init__module.so
# trying /usr/lib/python2.7/dist-packages/gi/__init__.py
# /usr/lib/python2.7/dist-packages/gi/__init__.pyc matches /usr/lib/python2.7/dist-packages/gi/__init__.py
import gi # precompiled from /usr/lib/python2.7/dist-packages/gi/__init__.pyc
# trying __future__.so
# trying __future__module.so
# trying __future__.py
# trying __future__.pyc
# trying /usr/lib/python2.7/__future__.so
# trying /usr/lib/python2.7/__future__module.so
# trying /usr/lib/python2.7/__future__.py
# /usr/lib/python2.7/__future__.pyc matches /usr/lib/python2.7/__future__.py
import __future__ # precompiled from /usr/lib/python2.7/__future__.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_gi.so
dlopen("/usr/lib/python2.7/dist-packages/gi/_gi.so", 2);
import gi._gobject # directory /usr/lib/python2.7/dist-packages/gi/_gobject
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/__init__.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/__init__module.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/__init__.py
# /usr/lib/python2.7/dist-packages/gi/_gobject/__init__.pyc matches /usr/lib/python2.7/dist-packages/gi/_gobject/__init__.py
import gi._gobject # precompiled from /usr/lib/python2.7/dist-packages/gi/_gobject/__init__.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/sys.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/sysmodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/sys.py
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/sys.pyc
import gi._glib # directory /usr/lib/python2.7/dist-packages/gi/_glib
# trying /usr/lib/python2.7/dist-packages/gi/_glib/__init__.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/__init__module.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/__init__.py
# /usr/lib/python2.7/dist-packages/gi/_glib/__init__.pyc matches /usr/lib/python2.7/dist-packages/gi/_glib/__init__.py
import gi._glib # precompiled from /usr/lib/python2.7/dist-packages/gi/_glib/__init__.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_glib/_glib.so
dlopen("/usr/lib/python2.7/dist-packages/gi/_glib/_glib.so", 2);
import gi._glib._glib # dynamically loaded from /usr/lib/python2.7/dist-packages/gi/_glib/_glib.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/option.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/optionmodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/option.py
# /usr/lib/python2.7/dist-packages/gi/_glib/option.pyc matches /usr/lib/python2.7/dist-packages/gi/_glib/option.py
import gi._glib.option # precompiled from /usr/lib/python2.7/dist-packages/gi/_glib/option.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_glib/sys.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/sysmodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/sys.py
# trying /usr/lib/python2.7/dist-packages/gi/_glib/sys.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_glib/optparse.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/optparsemodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/optparse.py
# trying /usr/lib/python2.7/dist-packages/gi/_glib/optparse.pyc
# trying optparse.so
# trying optparsemodule.so
# trying optparse.py
# trying optparse.pyc
# trying /usr/lib/python2.7/optparse.so
# trying /usr/lib/python2.7/optparsemodule.so
# trying /usr/lib/python2.7/optparse.py
# /usr/lib/python2.7/optparse.pyc matches /usr/lib/python2.7/optparse.py
import optparse # precompiled from /usr/lib/python2.7/optparse.pyc
# trying textwrap.so
# trying textwrapmodule.so
# trying textwrap.py
# trying textwrap.pyc
# trying /usr/lib/python2.7/textwrap.so
# trying /usr/lib/python2.7/textwrapmodule.so
# trying /usr/lib/python2.7/textwrap.py
# /usr/lib/python2.7/textwrap.pyc matches /usr/lib/python2.7/textwrap.py
import textwrap # precompiled from /usr/lib/python2.7/textwrap.pyc
# trying string.so
# trying stringmodule.so
# trying string.py
# trying string.pyc
# trying /usr/lib/python2.7/string.so
# trying /usr/lib/python2.7/stringmodule.so
# trying /usr/lib/python2.7/string.py
# /usr/lib/python2.7/string.pyc matches /usr/lib/python2.7/string.py
import string # precompiled from /usr/lib/python2.7/string.pyc
import strop # builtin
# trying gettext.so
# trying gettextmodule.so
# trying gettext.py
# trying gettext.pyc
# trying /usr/lib/python2.7/gettext.so
# trying /usr/lib/python2.7/gettextmodule.so
# trying /usr/lib/python2.7/gettext.py
# /usr/lib/python2.7/gettext.pyc matches /usr/lib/python2.7/gettext.py
import gettext # precompiled from /usr/lib/python2.7/gettext.pyc
# trying locale.so
# trying localemodule.so
# trying locale.py
# trying locale.pyc
# trying /usr/lib/python2.7/locale.so
# trying /usr/lib/python2.7/localemodule.so
# trying /usr/lib/python2.7/locale.py
# /usr/lib/python2.7/locale.pyc matches /usr/lib/python2.7/locale.py
import locale # precompiled from /usr/lib/python2.7/locale.pyc
import operator # builtin
# trying functools.so
# trying functoolsmodule.so
# trying functools.py
# trying functools.pyc
# trying /usr/lib/python2.7/functools.so
# trying /usr/lib/python2.7/functoolsmodule.so
# trying /usr/lib/python2.7/functools.py
# /usr/lib/python2.7/functools.pyc matches /usr/lib/python2.7/functools.py
import functools # precompiled from /usr/lib/python2.7/functools.pyc
import _functools # builtin
import _locale # builtin
# trying copy.so
# trying copymodule.so
# trying copy.py
# trying copy.pyc
# trying /usr/lib/python2.7/copy.so
# trying /usr/lib/python2.7/copymodule.so
# trying /usr/lib/python2.7/copy.py
# /usr/lib/python2.7/copy.pyc matches /usr/lib/python2.7/copy.py
import copy # precompiled from /usr/lib/python2.7/copy.pyc
# trying weakref.so
# trying weakrefmodule.so
# trying weakref.py
# trying weakref.pyc
# trying /usr/lib/python2.7/weakref.so
# trying /usr/lib/python2.7/weakrefmodule.so
# trying /usr/lib/python2.7/weakref.py
# /usr/lib/python2.7/weakref.pyc matches /usr/lib/python2.7/weakref.py
import weakref # precompiled from /usr/lib/python2.7/weakref.pyc
# trying org.so
# trying orgmodule.so
# trying org.py
# trying org.pyc
# trying /usr/lib/python2.7/org.so
# trying /usr/lib/python2.7/orgmodule.so
# trying /usr/lib/python2.7/org.py
# trying /usr/lib/python2.7/org.pyc
# trying /usr/lib/python2.7/plat-linux2/org.so
# trying /usr/lib/python2.7/plat-linux2/orgmodule.so
# trying /usr/lib/python2.7/plat-linux2/org.py
# trying /usr/lib/python2.7/plat-linux2/org.pyc
# trying /usr/lib/python2.7/lib-tk/org.so
# trying /usr/lib/python2.7/lib-tk/orgmodule.so
# trying /usr/lib/python2.7/lib-tk/org.py
# trying /usr/lib/python2.7/lib-tk/org.pyc
# trying /usr/lib/python2.7/lib-dynload/org.so
# trying /usr/lib/python2.7/lib-dynload/orgmodule.so
# trying /usr/lib/python2.7/lib-dynload/org.py
# trying /usr/lib/python2.7/lib-dynload/org.pyc
# trying /usr/local/lib/python2.7/dist-packages/org.so
# trying /usr/local/lib/python2.7/dist-packages/orgmodule.so
# trying /usr/local/lib/python2.7/dist-packages/org.py
# trying /usr/local/lib/python2.7/dist-packages/org.pyc
# trying /usr/lib/python2.7/dist-packages/org.so
# trying /usr/lib/python2.7/dist-packages/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/org.py
# trying /usr/lib/python2.7/dist-packages/org.pyc
# trying /usr/lib/python2.7/dist-packages/PIL/org.so
# trying /usr/lib/python2.7/dist-packages/PIL/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/PIL/org.py
# trying /usr/lib/python2.7/dist-packages/PIL/org.pyc
# trying /usr/lib/python2.7/dist-packages/gst-0.10/org.so
# trying /usr/lib/python2.7/dist-packages/gst-0.10/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/gst-0.10/org.py
# trying /usr/lib/python2.7/dist-packages/gst-0.10/org.pyc
# trying /usr/lib/python2.7/dist-packages/gtk-2.0/org.so
# trying /usr/lib/python2.7/dist-packages/gtk-2.0/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/gtk-2.0/org.py
# trying /usr/lib/python2.7/dist-packages/gtk-2.0/org.pyc
# trying /usr/lib/pymodules/python2.7/org.so
# trying /usr/lib/pymodules/python2.7/orgmodule.so
# trying /usr/lib/pymodules/python2.7/org.py
# trying /usr/lib/pymodules/python2.7/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntu-sso-client/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntu-sso-client/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntu-sso-client/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntu-sso-client/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntuone-client/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-client/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-client/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntuone-client/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntuone-control-panel/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-control-panel/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-control-panel/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntuone-control-panel/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntuone-couch/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-couch/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-couch/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntuone-couch/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntuone-installer/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-installer/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-installer/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntuone-installer/org.pyc
# trying /usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol/org.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol/orgmodule.so
# trying /usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol/org.py
# trying /usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol/org.pyc
# trying struct.so
# trying structmodule.so
# trying struct.py
# trying struct.pyc
# trying /usr/lib/python2.7/struct.so
# trying /usr/lib/python2.7/structmodule.so
# trying /usr/lib/python2.7/struct.py
# /usr/lib/python2.7/struct.pyc matches /usr/lib/python2.7/struct.py
import struct # precompiled from /usr/lib/python2.7/struct.pyc
import _struct # builtin
# trying /usr/lib/python2.7/dist-packages/gi/_glib/gi.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/gimodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_glib/gi.py
# trying /usr/lib/python2.7/dist-packages/gi/_glib/gi.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/constants.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/constantsmodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/constants.py
# /usr/lib/python2.7/dist-packages/gi/_gobject/constants.pyc matches /usr/lib/python2.7/dist-packages/gi/_gobject/constants.py
import gi._gobject.constants # precompiled from /usr/lib/python2.7/dist-packages/gi/_gobject/constants.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/_gobject.so
dlopen("/usr/lib/python2.7/dist-packages/gi/_gobject/_gobject.so", 2);
import gi._gobject._gobject # dynamically loaded from /usr/lib/python2.7/dist-packages/gi/_gobject/_gobject.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelper.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelpermodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelper.py
# /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelper.pyc matches /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelper.py
import gi._gobject.propertyhelper # precompiled from /usr/lib/python2.7/dist-packages/gi/_gobject/propertyhelper.pyc
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrappermodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.py
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.pyc
# trying datetime.so
# trying datetimemodule.so
# trying datetime.py
# trying datetime.pyc
# trying /usr/lib/python2.7/datetime.so
# trying /usr/lib/python2.7/datetimemodule.so
# trying /usr/lib/python2.7/datetime.py
# trying /usr/lib/python2.7/datetime.pyc
# trying /usr/lib/python2.7/plat-linux2/datetime.so
# trying /usr/lib/python2.7/plat-linux2/datetimemodule.so
# trying /usr/lib/python2.7/plat-linux2/datetime.py
# trying /usr/lib/python2.7/plat-linux2/datetime.pyc
# trying /usr/lib/python2.7/lib-tk/datetime.so
# trying /usr/lib/python2.7/lib-tk/datetimemodule.so
# trying /usr/lib/python2.7/lib-tk/datetime.py
# trying /usr/lib/python2.7/lib-tk/datetime.pyc
# trying /usr/lib/python2.7/lib-dynload/datetime.so
dlopen("/usr/lib/python2.7/lib-dynload/datetime.so", 2);
import datetime # dynamically loaded from /usr/lib/python2.7/lib-dynload/datetime.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrappermodule.so
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.py
# trying /usr/lib/python2.7/dist-packages/gi/_gobject/GTypeWrapper.pyc
import gi._gi # dynamically loaded from /usr/lib/python2.7/dist-packages/gi/_gi.so
1

In python environment, all's about dependencies, like in C. But if you install the module through PyPI (pip) or the system packaging tool (apt, yum, brew, etc), this matters the way all python scripts get further invoked. e.g. install PyGObject

A. (Choose One or the Other) through package tool (this may not work on several occasions, apt might be disabled or python version change) :

    sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0

or B. (Choose One or the Other) through PyPi (this should always work)

    cat <<EOF | tee requirements.txt
    pycairo
    PyGObject
    EOF
    pip3 install -r requirements.txt 

A. Python libraries get installed through common package tools, and further update will disrupt the dependencies system, if not using the standard python3- package :

    python3
    >>> import gi.repository

B. (Recommended for developers) Python modules might get installed within a virtual environment (virtualenv create /usr/src/venv). Hence it will be found within a shell block starting with virtualenv enabled:

    source /usr/src/venv/bin/activate
    pip3 install -r requirements.txt
    python3
    >>> import gi.repository

requirements.txt contains the list of python modules you want to add to the PYTHONPATH. The virtualenv ensures every single required module is available from desired Python version and library and has precedence over system package tool manager.

0

In my case, I was using Python3.6 and recently updated to Python3.7, then this problem occurred. This solution works for me:

cd /usr/lib/python3/dist-packages/gi
sudo ln -s _gi.cpython-{36m,37m}-x86_64-linux-gnu.so
0

This worked for me on Ubuntu 20.04, python 3.8 using a virtual environment.

For convenience, I quote:

..but I realize that my Python's version was the 3.6 and the gi was installed on the python 3 directory, because I installed via this command "sudo apt-get install python3-gi" so I run that command on my terminal:

ln -s /usr/lib/python3/dist-packages/gi /home/your_name_goes_here/miniconda3/lib/python3.6/site-packages/

(P.S.: in "your_user_goes_here", you put your username)

In fact, for correctness (as I don't use conda) I altered the command to:

$ ln -s /usr/lib/python3/dist-packages/gi /home/your_name_goes_here/your_python_path/3.8/lib/python3.8/site-packages
0

For Ubuntu 16.04 running Python3.6 via the deadsnakes PPA.

Symlink the distribution binaries (3.5) to new names (3.6):

cd /usr/lib/python3/dist-packages/gi
sudo ln -s _gi.cpython-35m-x86_64-linux-gnu.so        _gi.cpython-36m-x86_64-linux-gnu.so 
sudo ln -s _gi_cairo.cpython-35m-x86_64-linux-gnu.so  _gi_cairo.cpython-36m-x86_64-linux-gnu.so
0

Had the same issue within a docker container building with pyinstaller and nuitka on aarch64 architecture. Setting

ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/aarch64-linux-gnu
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/aarch64-linux-gnu/girepository-1.0

fixed the issue

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .