简体   繁体   中英

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

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

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

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM