90

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.

19 Answers 19

69

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.

8
  • 22
    It's almost always preferable to reinstall the package in one step using sudo apt install --reinstall <package-name>, because this avoids calculating and removing dependencies (and marking packages for removal). Be aware that removing packages is not always a reversible procedure; for example APT will remove itself or its dependencies if asked to! The --reinstall flag is therefore much safer than remove followed by install. The remove command by itself does not remove configuration files of a package so has no benefit over install --reinstall.
    – Zanna
    Aug 8, 2018 at 7:58
  • 6
    This answer should be updated with the suggestion by @Zanna, removing python3-apt will remove a ton of dependencies and may scare the crap out of someone, not to mention messing up their system.
    – alkanen
    Aug 1, 2019 at 8:08
  • So uninstall apt-package with sudo dpkg -r --force-depends apt-package instead which uninstalls only apt-package but not any of its dependencies.
    – karel
    Aug 1, 2019 at 12:15
  • 13
    i had to use sudo apt remove --purge python3-apt then sudo apt install python3-apt, the weird thing is that after reinstalling, 78 really unrelated(well I don't really know) packages were marked as no longer needed and can be autoremove.. I'm kind of concerned but at least it fixed my problem haha
    – Kenivia
    Apr 8, 2020 at 23:54
  • 1
    @Kenivia I tried that command and aborted immmediately - it was about to remove half of my desktop. I guess that's why you also had so many no longer needed packages.
    – xeruf
    May 23, 2020 at 9:50
48
update-alternatives  --set python3  /usr/bin/python3.6
6
  • 6
    Updating alternatives for python might sometimes break things.
    – Kulfy
    Apr 29, 2019 at 11:06
  • 2
    I used update-alternatives --set python3 /usr/bin/python3.5 and it worked. python3 was already pointing to python3.6 on my system.
    – Desik
    Jun 26, 2019 at 20:06
  • This is exactly what worked for me. I had just followed a guide on how to update Python 3 in Ubuntu 18.04 and my apt-get no longer worked. No idea that would happen. Feb 5, 2020 at 19:36
  • It worked, just use sudo. Feb 18, 2021 at 16:15
  • 3
    update-alternatives --set python3 /usr/bin/python3.8 solved my problem on Ubuntu 20.04
    – umitu
    Jun 9, 2022 at 8:24
34

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!

3
  • 7
    Thanks, this helped. Instead of doing a cp, I used ln -s instead. So sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so.
    – erwaman
    May 21, 2020 at 19:43
  • Thanks. Worked for me. Jan 21, 2021 at 7:39
  • Thanks. sudo cp apt_pkg.cpython-34m-x86_64-linux-gnu.so apt_pkg.so worked for me Jan 27, 2021 at 4:59
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

2
  • 4
    As mentioned in a comment over at the "original solution," for me this worked: sudo ln -s apt_pkg.cpython-{35m,36m}-x86_64-linux-gnu.so. The 35 (python 3.5) version is what's installed regardless of the particular sub-version of python 3.x that you're running. Thanks!
    – fred271828
    May 23, 2019 at 20:26
  • 1
    I think this worked for me when I upgraded to either Python 3.6 or 3.7, but now I have Python 3.8 and the expected file name seems to have changed to just apt_pkg.so. See askubuntu.com/a/1154616/725987 Mar 30, 2020 at 20:29
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

5
  • 7
    it removed half my system software on zorin OS, and installing doesn't get them back Jul 19, 2021 at 20:30
  • This solve the issue, I've replaced the default python/pip 3.8 with python/pip 3.7, and end up by damaging the default pip causing this error on ubuntu 20.
    – Ibrahim.H
    Oct 25, 2021 at 13:17
  • 3
    dangerous and stupid. Do not do this. Tons of system software relies on python3-apt including apt itself. You won't be able to re-install again because the apt command will error.
    – Rugnir
    Oct 28, 2022 at 12:26
  • 1
    Used this on Plesk and now nothing works Jan 30, 2023 at 13:04
  • I changed this to reinstall rather than remove install so that it doesn't remove anything (including dependencies), it simply reinstalls the package safely.
    – mchid
    Aug 16, 2023 at 7:45
8

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!

2
  • Worked for me thanks :) Sep 28, 2021 at 15:27
  • @dave-yarwood saved my day. "new expected filename is just apt_pkg.so"
    – gue22
    Oct 19, 2023 at 6:22
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
4
  • Thanks. I had already tried but it didn't work. I tried once more now, getting, after apt-get update, the warning W: Failed to fetch bzip2:/var/lib/apt/lists/partial/it.archive.ubuntu.com_ubuntu_dists_saucy-updates_main_source_Sources Hash Sum mismatch
    – user291925
    Jun 9, 2014 at 14:22
  • K - you have a problem with the update sources. I've seen several threads about how to fix that The following link seems to be a good match for your question askubuntu.com/questions/41605/… Jun 9, 2014 at 14:28
  • Thanks! I tried, but I still get the same error when running update-manager
    – user291925
    Jun 9, 2014 at 15:12
  • K - that about exhausts my possibilities with out a more extensive web search. Good luck! Jun 9, 2014 at 15:22
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.

7
  • 1
    I'm also facing the issue because I updated to python3.7 in Ubuntu 18. But when I do what you did (create symbolic link), I got ln: failed to create symbolic link '/usr/bin/python3': File exists. If I do ls in my /usr/bin/python*, I have python3, python3.6, python3.6m, python3.7, python3.7m and python3m. If I do sudo update-alternatives --config python3, I have *0 /usr/bin/python3.7 2 auto mode, 1 /usr/bin/python3.6 1 manual mode and 2 /usr/bin/python3.7 2 manual mode. Thank you. Oct 23, 2019 at 2:35
  • may be you need to delete a link file already exist or rename it to .old and create your own link.
    – M.Hefny
    Oct 23, 2019 at 12:47
  • 2
    @user1330974 apparently, just forcing the symbolic link worked: ln -sf /usr/bin/python3.6 /usr/bin/python3
    – dallonsi
    Nov 20, 2019 at 10:07
  • 1
    @dallonsi Thank you. I didn't look into it further, but I'll be using your approach. :) Nov 21, 2019 at 12:41
  • 1
    you should prefer update-alternatives, which will do this in a safer way
    – xeruf
    May 23, 2020 at 9:55
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.

0
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

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.

1
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

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
1
  • If I copied the file instead of creating a symlink, do you think that would interfere with update-alternatives, or maybe cause some other headaches for me down the road? May 5, 2022 at 2:51
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
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
2
  • So you removed one version for 3.4 ? Jul 20, 2018 at 13:49
  • Tried this, and found it to make things worse. Cannot recommend.
    – MERose
    Jan 7, 2020 at 16:30
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...

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
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
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .