63

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.

15 Answers 15

54

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.

7
  • 18
    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 '18 at 7:58
  • 5
    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 '19 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 '19 at 12:15
  • 6
    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 '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 9:50
32
update-alternatives  --set python3  /usr/bin/python3.6
5
  • 4
    Updating alternatives for python might sometimes break things.
    – Kulfy
    Apr 29 '19 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 '19 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 '20 at 19:36
  • It worked, just use sudo. Feb 18 at 16:15
  • I want to use Python 3.9 as my default though Aug 13 at 20:09
23

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
  • 3
    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 askubuntu.com/a/1154616/725987 Mar 30 '20 at 20:29
16

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
  • 2
    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 '20 at 19:43
  • Thanks. Worked for me. Jan 21 at 7:39
  • Thanks. sudo cp apt_pkg.cpython-34m-x86_64-linux-gnu.so apt_pkg.so worked for me Jan 27 at 4:59
8

For me this did the trick:

apt-get remove  python3-apt
apt-get install python3-apt

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

1
  • 1
    it removed half my system software on zorin OS, and installing doesn't get them back Jul 19 at 20:30
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
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 '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 askubuntu.com/questions/41605/… Jun 9 '14 at 14:28
  • Thanks! I tried, but I still get the same error when running update-manager
    – user291925
    Jun 9 '14 at 15:12
  • K - that about exhausts my possibilities with out a more extensive web search. Good luck! Jun 9 '14 at 15:22
4

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 '19 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 '19 at 12:47
  • 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. :) 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 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.

2

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!

1
1

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 '18 at 13:49
  • Tried this, and found it to make things worse. Cannot recommend.
    – MERose
    Jan 7 '20 at 16:30
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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.