91

I'm rather new to Ubuntu and I'm trying to run update-manager on Ubuntu 13.10. I get:

jacopo@jacopo-laptop:~$ update-manager 
Traceback (most recent call last):
  File "/usr/bin/update-manager", line 28, in <module>
    from gi.repository import Gtk
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 27, in <module> from ._gi import _API
ImportError: No module named 'gi._gi'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 64, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 20, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 21, in <module>
import apt_pkg
ImportError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
File "/usr/bin/update-manager", line 28, in <module>
from gi.repository import Gtk
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 27, in <module>
from ._gi import _API
ImportError: No module named 'gi._gi'

As far as I have understood, this is a problem related to the fact that I should have python3.4 installed. I installed it in /usr/local/lib, but I always get the same error. In this way I can't even upgrade to Ubuntu 14.04.

karel
  • 122,292
  • 133
  • 301
  • 332
user291925
  • 921
  • 1
  • 6
  • 4

19 Answers19

70

Reinstall apt_pkg using:

sudo apt-get install --reinstall python3-apt

The error is primarily because of library apt_pkg.cpython-35m-x86_64-linux-gnu.so not being present in /usr/lib/python3/dist-packages.

Removing and reinstalling should help.

Eliah Kagan
  • 119,640
48
update-alternatives  --set python3  /usr/bin/python3.6
Kulfy
  • 18,154
v12aml
  • 591
  • 4
  • 3
43

The following solution worked for me:

cd  /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

If you get an error message saying too many levels of symbolic links as shown below:

cp: failed to access '/usr/lib/python3/dist-packages/apt_pkg.so': Too many levels of symbolic links

Then you need to simply unlink the apt_pkg.so file. Use the following command:

sudo unlink apt_pkg.so

And then use the command

sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Hope this helps!

28

for me the following steps worked:

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

the original solution is here: https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg/36232975#36232975

26

For me this did the trick:

sudo apt install --reinstall python3-apt

This problem actually is similar to this one: apt-get broken: No module named debian.deb822

mchid
  • 44,904
  • 8
  • 102
  • 162
amagard
  • 361
9

This solution used to work for me after I had upgrade to either Python 3.6 or 3.7 and add-apt-repository stopped working:

cd /usr/lib/python3/dist-packages
# where 35m is the file you have and 38m corresponds to your Python version
sudo ln -s apt_pkg.cpython-{35m,38m}-x86_64-linux-gnu.so

Now I have Python 3.8 and add-apt-repository stopped working again. I found another, related SO question, with this answer that worked for me. It appears that the expected filename is now just apt_pkg.so, so you have to do this:

cd /usr/lib/python3/dist-packages
# where 35m is the file you have
sudo ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

The fact that the new expected filename is just apt_pkg.so is promising. Hopefully it means we won't have to keep doing this dance every time there's a new minor version of Python!

5

The problem for me was that I installed python3.7 next to python3.6 and made it the default.

I manually run the following command:

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

so python3 now points to the correct version of python ... that solves the issue.

M.Hefny
  • 189
  • 1
  • 2
5

If you're trying to upgrade 13.10 to 14.04, try following these instructions. If you're just trying to update your current system from the command line, open a terminal and type the following:

sudo apt-get update
sudo apt-get dist-upgrade
Charles Green
  • 21,859
2

For me below was worth full

    mansoor@LDEVOPS-MANSOOR:~/Documents/clients/HR/DevopsSimulator$  cd /usr/lib/python3/dist-packages
mansoor@LDEVOPS-MANSOOR:/usr/lib/python3/dist-packages$ sudo ln -s apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

2

I had the same issue after upgrading to python 3.9, and recreating the symlink '/usr/bin/python3' to point to the new location.

The only solution that worked for me, was that from a comment on the first answer, from @Kenivia: sudo apt remove --purge python3-apt then sudo apt install python3-apt

This solved the error but uninstalled my ubuntu Software Center. But it was not a big deal, because I just reinstalled it with sudo apt install ubuntu-software and now everything seems to be working just fine

EDIT: some issue still persisted after this (update-manger kept giving errors and wouldn't start). So I had to go back and revert the symlink /usr/bin/python3 to point to the original python3.6 location. This solved everything

stann1
  • 121
2

Running Ubuntu 22.10 with Python 3.10.7 my apt package manager stopped working as well with apt reporting that the apt_pkg was not installed. What worked for me was the following:

 cd /usr/lib/python3/dist-packages/
 ln -s apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so
 apt-get install --reinstall python3-apt -y
 init 6

Did the trick for me.

gh0st
  • 41
2

I have managed to solve this by copying apt_pkg.cpython-34m-i386-linux-gnu.so to /usr/lib/python3/dist-packages/ from another desktop running Ubuntu 14.04 LTS. I have tried almost all possibilities found over internet like, purging python installation and then reinstalling, cleaning broken upgrade files etc, but ended up with no success.

1

Just an advise: I avoid installing "my" python on /usr/local, but I have one dedicated directory per python version (e.g. in /opt) and I'm using virtual environments on my user account to use the python I need, which is activated by my .profile

By this approach, you will always avoid to change the python system expected configuration and dependencies.

Well, I know, it's too late for your case, but...

1

Weird, I ended up with multiple versions of python 3.9, I had 3.9 and 3.9.12 in ls /usr/bin/pyth* when I checked my version I was on 3.9.12 but sudo update-alternatives --list only showed version 3.9. What I did was

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

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1

That got my sudo apt upgrade working, then I was able to just copy the .so file to apt_pkg.so

#You might need this if the package isn't there
#sudo apt-get install python3-apt --reinstall

cd /usr/lib/python3/dist-packages sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so sudo apt upgrade

1

In my Ubuntu, the default python was Python3.6, which I updated my default python into 3.9, from there I started facing the same issue.

python3 --version Will give you a good insight about which version is current default.

After going through the threads here, What I did was examined /usr/lib/python3/dist-packages

cd  /usr/lib/python3/dist-packages;
ls

found the file:

apt_pkg.cpython-36m-x86_64-linux-gnu.so

from where I understood, the issue was because, I have only dist-packages against cpython-36m but current default is 3.9 which do not have a dist package, For A quick remedy, instead of installing dist-package for 3.9, I roll back update-alternative to python3.6.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
0

I faced same problem after upgrade ubuntu 19 to ubuntu 20. In ubuntu 20 default python version is 3.8 But some packages doesn't support this version. For this I downgraded default python version to 3.7 After downgrade I faced this shit problem.

This is downgrade documentation: https://blog.nixarsoft.com/2020/06/04/ubuntu-20-and-old-python-versions/

Let me tell you how to solve this problem.

python3.8
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import apt_pkg
>>> apt_pkg
<module 'apt_pkg' from '/usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so'>
>>> exit

As you can see there is a ".so" file for apt_pkg and it is located to "/usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so" Now if you link this file to "/usr/lib/python3/dist-packages/apt_pkg.cpython-37-x86_64-linux-gnu.so" then you can use this library in python 3.7. Please take care that I changed "38" to "37" in linked file. You must be root for make link.

sudo ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-37-x86_64-linux-gnu.so

Probably you can do same method for other python versions. If you change 38 to 36 then you can use this library in python 3.6...

kodmanyagha
  • 148
  • 11
0

I tried using @kodmanyagha 's answer, but no apt_pkg was found in the python environment. I solved it thanks to @saranjeet 's answer:

 /usr/lib/python3/dist-packages$ sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
Tzahi Leh
  • 281
0

I also encountered this issue, solved it by modify the shebang of /usr/bin/add-apt-repository from:

#!/usr/bin/python3

to:

#!/usr/bin/python3.6
uriya
  • 1
0

Just in case it helps another, I finally solved this problem, that was apparently caused by python version conflicts, by redirecting the link python3, then redirecting it to the right python version:

sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.4

You may need to enter the correct python version, found with

python3 -V