85

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
  • 110,292
  • 102
  • 269
  • 299
user291925
  • 861
  • 1
  • 6
  • 4

18 Answers18

64

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
  • 116,445
  • 54
  • 318
  • 493
vikas kapdoskar
  • 773
  • 5
  • 2
  • 22
    It's almost always preferable to reinstall the package in one step using `sudo apt install --reinstall `, 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 08 '18 at 07: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 01 '19 at 08: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 01 '19 at 12:15
  • 11
    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 08 '20 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 '20 at 09:50
  • I have `/usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so` but am getting that error - the reinstall changes nothing. – xeruf May 23 '20 at 09:54
  • @Xerus in my case I have the error and the package in the right place: `ls /usr/lib/python3/dist-packages/apt_pkg.* /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so` – loretoparisi Jan 29 '21 at 09:03
45
update-alternatives  --set python3  /usr/bin/python3.6
Kulfy
  • 17,416
  • 26
  • 64
  • 103
v12aml
  • 561
  • 4
  • 3
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

  • 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 '19 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 https://askubuntu.com/a/1154616/725987 – Dave Yarwood Mar 30 '20 at 20:29
28

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!

yksolanki9
  • 380
  • 4
  • 6
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
  • 42,315
  • 7
  • 94
  • 147
amagard
  • 361
  • 3
  • 4
  • 7
    it removed half my system software on zorin OS, and installing doesn't get them back – DaniyalAhmadSE Jul 19 '21 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 '21 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 '22 at 12:26
  • 1
    Used this on Plesk and now nothing works – Atif Bashir Jan 30 '23 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 '23 at 07: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!

Dave Yarwood
  • 185
  • 1
  • 6
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
  • 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. – user1330974 Oct 23 '19 at 02: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 '19 at 12:47
  • @user1330974 did you finally solve the problem ? I'm facing the same issue – dallonsi Nov 20 '19 at 09:57
  • 2
    @user1330974 apparently, just forcing the symbolic link worked: `ln -sf /usr/bin/python3.6 /usr/bin/python3` – dallonsi Nov 20 '19 at 10:07
  • 1
    @dallonsi Thank you. I didn't look into it further, but I'll be using your approach. :) – user1330974 Nov 21 '19 at 12:41
  • 1
    you should prefer `update-alternatives`, which will do this in a safer way – xeruf May 23 '20 at 09:55
  • when `update-alternatives` fails with `error: no alternatives for python3` this the way to go. Worked for me with `python3.5` since I was upgrading Ubuntu 16.04. – Xvolks Oct 13 '21 at 09:38
4

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
  • 20,952
  • 21
  • 60
  • 92
  • 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 09 '14 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 http://askubuntu.com/questions/41605/trouble-downloading-updates-due-to-a-hash-sum-mismatch-error – Charles Green Jun 09 '14 at 14:28
  • Thanks! I tried, but I still get the same error when running `update-manager` – user291925 Jun 09 '14 at 15:12
  • K - that about exhausts my possibilities with out a more extensive web search. Good luck! – Charles Green Jun 09 '14 at 15:22
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
  • 3
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...

Michael Hooreman
  • 481
  • 3
  • 13
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
Mansur Ul Hasan
  • 291
  • 4
  • 5
  • 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? – DeadlyChambers May 05 '22 at 02: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

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
  • 150
  • 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
  • 261
  • 3
  • 6
  • 17
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

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
  • 21
  • 3
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
matthias2t
  • 37
  • 1