Cannot import name '_gi'

Question:

I’m trying to add a repository to ppa with the add-apt-repository commands but the _gi module from Python is not found.

I did this command : sudo add-apt-repository ppa:s-mankowski/ppa-kf5

Here is the traceback :

Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 67, in <module>
    from gi.repository import Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-packages/gi/__init__.py)

I’m on Ubuntu with Python3.7, I tried many solutions like but it doesn’t work :

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

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

I can’t use the sudo add-apt-repository ppa:s-mankowski/ppa-kf5 command but running a Python file with python3 {file} works.

Thanks for the help !

Asked By: Hugo Sohm

||

Answers:

Ubuntu does not like to switch its default interpreter away from python 3.7.

So switch it back to 3.6 by using

sudo update-alternatives --config python3

After that try to install the gi package:

sudo apt install python3-gi
Answered By: M. Spiller

Thanks to a comment by Wilhelm, I found that the solution is:

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

This worked for me with Python 3.7 on Ubuntu.

Answered By: pranaychenna

Follow the below steps to solve the issue.

(1) ls -al /usr/bin | grep python

(2) sudo gedit /usr/bin/add-apt-repository

Then change the !/usr/bin/python3 to

!/usr/bin/python3.6
Answered By: niwantha

These 2 steps may help you

$ cd /usr/lib/python3/dist-packages/gi
$ sudo ln -s _gi.cpython-{36m,37m}-x86_64-linux-gnu.so
Answered By: Bendy Latortue

This is an old issue, but if someone needs a quick solution , that doesn’t imply changing the default Python versions anywhere, just:

sudo python3.6 /usr/bin/apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
Answered By: Souza

when you check sudo apt install python3-gi it says "it’s already installed to newest version"
but your import gi is not working, so you should check its directory

cd /usr/lib/python3/dist-packages/gi

and you will see which version of python you are using in my case: "_gi.cpython-38-x86_64-linux-gnu.so" that is 38 => python3.8
so you should check sudo update-alternatives --config python3 to see if there is python3.8 to point or not, if not simply do:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

and make sure your python3 is pointing to python3.8
you can check it by: ls -l /etc/alternatives/python3
Done.

Answered By: Mohammad Amirpanah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.