'ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import)

Looks like I have broken my python installation when I wanted to switch to python 3.8. Using Ubuntu 18.04. Trying to use the gi, gives the following error:

$ python
Python 3.8.1 (default, Dec 31 2019, 18:42:42) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> from gi.repository import GLib, Gio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)

Tried running update-alternatives for python, but it tells me there is only one python alternative configured (3.8).

Tried to reinstall python3-gi and python3.8. Still the same problem



Solution 1:[1]

I had the same issue. I linked python3 to python3.6, for me it was pointing to 3.8. That solved the issue.

cd /usr/bin/
rm python3
ln -s python3.6 python3

Thats all. Now my system started working fine.

Solution 2:[2]

Install gi for python 3.8: python3.8 -m pip install pgi Then, instead of import gi use:

import pgi
pgi.install_as_gi()
from gi.repository import GLib, Gio

Alternatively, you can force install PyGObject for python 3.8:

sudo python3.8 -m pip install --ignore-installed PyGObject

which should allow one to from gi import ... as before.

Solution 3:[3]

For me the workaround was to create a symlink:

cd /usr/lib/python3/dist-packages/gi/
sudo ln -s _gi.so _gi.cpython-38-x86_64-linux-gnu.so

and it solved the problem for me.

Solution 4:[4]

Found answer here https://bugzilla.redhat.com/show_bug.cgi?id=1709787:

The cause is - /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so has incorrect name:

sh-5.0# python3 -c 'from gi.repository import GLib' Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.8/site-packages/gi/init.py", line 42, in from . import _gi ImportError: cannot import name '_gi' from 'gi' (/usr/lib64/python3.8/site-packages/gi/init.py) sh-5.0# mv /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so sh-5.0# python3 -c 'from gi.repository import GLib'

Note that since 3.8.0a4, the "m" is not supposed to be there. Is it somehow hardcoded?

sh-5.0# python3-config --extension-suffix .cpython-38-x86_64-linux-gnu.so

in my case it was

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

Solution 5:[5]

The reason of this error is that this app can't find the matched Python version of _gi_cairo.cpython-(version)-x86_64-linux-gnu.so.
Normally, this unmatched situation is caused by some wrong mixed usage of different versions of Python.

So basically, you can try to switch your Python version ( to the default version of your OS). Or you can go to '/usr/lib/python3/dist-packages/gi' and create a new .so library file:

cp _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi_cairo.cpython-(new version)-x86_64-linux-gnu.so

or

ln -s _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi.so

Solution 6:[6]

I had the same problem on ubuntu 18 as python3 was referring to python3.9. In order to solve it, I changed the alternative for python3:

sudo update-alternatives --config python3                                                                                                                                                                  
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.9   2         auto mode
  1            /usr/bin/python3.6   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

By choosing number 1, now python3 points to python3.6 and everything works fine again

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Austin
Solution 2
Solution 3 fgoudra
Solution 4 Andrey Starodubtsev
Solution 5 Flair
Solution 6 Yashar