34

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 !

Hugo Sohm
  • 2,872
  • 4
  • 23
  • 40

6 Answers6

56

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
pranaychenna
  • 571
  • 4
  • 2
  • 7
    Can you provide a short explanation of how this fixes the issue? – Leogout Jul 01 '20 at 08:31
  • This worked for me also. It's creating a symlink to the existing 3.6 library that is named as if it was a 3.7 library. My assumption is that that python 3.7 fails to load the 3.6 library and this fixes that in a similar way to https://askubuntu.com/a/1207369 – ita Aug 04 '20 at 05:00
  • 2
    This works for me too. I installed python 3.7 and 3.8 and had the same problem. Replacing `{36m,37m}` with `{35m,37m,38}` creates the two symlinks for `37m` and `38`. This allows me to open the `gnome-terminal` again. – mathiass Sep 10 '20 at 02:47
  • Also `sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-35m-x86_64-linux-gnu.so _gi.cpython-38-x86_64-linux-gnu.so` worked when switching from py 3.5 to py 3.8 – Szymek Feb 01 '21 at 08:51
  • had to do it for both gi/_gi.cpython gi/_gi_cairo.cpython and cairo/_cairo.cpython – Nonoreve Feb 24 '21 at 11:05
  • Thanks a lot it worked for me after trying this command post solution one. – Tanishq Vyas Mar 01 '21 at 19:25
  • 1
    Have in mind that you should be in the `/usr/lib/python3/dist-packages/gi/` directory when using the command so the link is created there. – PhoneixS Mar 08 '22 at 09:01
29

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
MSpiller
  • 3,500
  • 2
  • 12
  • 24
  • 1
    What is your python interpreter? I.e. when you type `python3`, which version is displayed? And can you type `import gi` in your python3 interpreter? – MSpiller Dec 18 '19 at 10:26
  • 1
    I'm using Python 3.7.5 with GCC 8.3, the Traceback is there when I write `import gi` – Hugo Sohm Dec 18 '19 at 10:27
  • Which version of Ubuntu? You could also try to: `python3 -m pip install gi` after uninstalling `python3-gi` – MSpiller Dec 18 '19 at 10:30
  • 1
    I'm using Ubuntu 18.04.3 LTS and the command `python3 -m pip install gi` doesn't works, It is returning `No matching distribution found for gi` – Hugo Sohm Dec 18 '19 at 10:36
  • Ubuntu does not really like to use another python interpreter for its system. You could try to switch back to python3.6 `sudo update-alternatives --config python3` and retry the installation. – MSpiller Dec 18 '19 at 10:40
  • It worked but it's hardly badly done, thanks M. Spiller – Hugo Sohm Dec 18 '19 at 10:56
  • so basically the only solution is to use 3.6? – njzk2 Apr 12 '20 at 19:51
  • 19
    A hacky fix to get it working with python3.7 `sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-{36m,37m}-x86_64-linux-gnu.so` – Wilhelm Apr 30 '20 at 03:30
14

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
Souza
  • 1,124
  • 5
  • 19
  • 44
  • 1
    Can you explain what this thing does? – fsevenm Aug 11 '21 at 05:18
  • 1
    @fsevenm it simply uses the python3.6 binary to run the apt-add-repository , solving the library's conflict existing on `python3.7+` . For me the issue was only appearing while adding the repo. So i didn't want to change between default versions of python. But you could change it back to an older version like the accepted answer suggests. – Souza Aug 11 '21 at 14:03
  • 1
    This solution worked for me on ubuntu 18.04 and I upgraded my python to 3.7 a while back. The other suggestions of "ln -s" don't make sense as they didn't even provide a target. Usage: ln -s source target. – oldpride Dec 04 '21 at 18:09
  • 1
    Best solution if you just want to run `apt-add-repository`. Leaves no side-effect. – Wei Shi Oct 13 '22 at 03:24
8

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
Bendy Latortue
  • 391
  • 5
  • 6
  • Thanks! but I'd like to know why would this happen and how to avoid it? I found this name error happened in many scene. – Sailist Mar 04 '21 at 07:48
3

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
niwantha
  • 51
  • 3
1

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.