Python can't find module named apt_pkg

12,240

Solution 1

In my case because I have 2 versions of python installed, python3.5 and python3.6 , the module wasn't found on the python3.6 version so what I did thanks to @Emmet suggestions was:

nano /usr/bin/add-apt-repository edited the line #! /usr/bin/python3 into #! /usr/bin/python3.5 and now add-apt-repository command works again.

Solution 2

It is important to understand that sometimes when you upgrade from an older python version some packages stay in the previous version path, so here is what I did:

cd /usr/lib/python3/dist-packages

check the existence of a file named apt_pkg.cpython-35m-x86_64-linux-gnu.so or 34m or 36m listing the files and when you find it, delete de current apt_pkg.so file

/usr/lib/python3/dist-packages# rm apt_pkg.so

finally create a link with the correct path using apt_pkg.so like this:

/usr/lib/python3/dist-packages# sudo ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

Now you can try again and It should work.

Share:
12,240

Related videos on Youtube

Sebastian
Author by

Sebastian

Updated on September 18, 2022

Comments

  • Sebastian
    Sebastian over 1 year

    I am running Ubuntu 16.04 and on my server I have 2 version of python , python3.5 and python3.6.

    What I already tried is:

    uninstalling python3-apt and reinstalling it, didn't help still the same error.

    EDIT: on python3 I have python3.6 running.

    Added error:

    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 27, in <module>
        import apt_pkg
    
    • Liso
      Liso about 5 years
      try sudo apt install python-apt
    • Sebastian
      Sebastian about 5 years
      @Emmet same thing, I also added the traceback in the post for a better view of the error.
    • Liso
      Liso about 5 years
    • Sebastian
      Sebastian about 5 years
      @Emmet for some reasons on python3.5 works fine wierd, thanks for you help.
  • Rajkumar Natarajan
    Rajkumar Natarajan almost 5 years
    I've updated python from version 3.5 to 3.7 in my ubuntu 16.04 and I faced this issue after that. Your answer solved my issue.
  • Mariusz Wiazowski
    Mariusz Wiazowski about 4 years
    Great! I had the same issue with identical traceback and your solution has definitely worked. Thanks!