Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 621
user3111525 Avatar asked Jan 21 '20 09:01

user3111525


4 Answers

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.

like image 72
salt-die Avatar answered Nov 01 '22 20:11

salt-die


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.

like image 17
Austin Avatar answered Nov 01 '22 22:11

Austin


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.

like image 3
fgoudra Avatar answered Nov 01 '22 20:11

fgoudra


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
like image 1
Andrey Starodubtsev Avatar answered Nov 01 '22 21:11

Andrey Starodubtsev