ModuleNotFoundError No module named apt_pkg

user1184294 picture user1184294 · Feb 8, 2018 · Viewed 17.2k times · Source

I am trying to add a repo using command

 sudo add-apt-repository ppa:gezakovacs/ppa

Following is complete 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
ModuleNotFoundError: No module named 'apt_pkg'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, 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 23, in <module>
    import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
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
ModuleNotFoundError: No module named 'apt_pkg'

getting above error.

I have install python2 , Python3.5 & Python3.6 installed on Xubuntu 16.04. Does it causing any issue???

Answer

K.S. picture K.S. · Feb 13, 2020

This is likely because you switched python versions in a weird way. Do you have /usr/lib/python3/dist-packages/apt_pkg.so ? This can be missing if you removed a version of python.

The answer that worked for me was this from askubuntu forums:

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