10

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

user1184294
  • 347
  • 1
  • 5
  • 11
  • Your question might be better suited for https://askubuntu.com/ –  Feb 08 '18 at 04:32
  • For starters though: do you have a file `/usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so`? –  Feb 08 '18 at 04:33
  • Thanks. Found the problem. My Python3 was linked to python3.6 but when changed to 3.5.2 started working. Maybe Python 3.6 is not fully supported yet – user1184294 Feb 08 '18 at 04:48
  • That's weird, because it never should have been linked that way in the first place. –  Feb 08 '18 at 06:11
  • And yes, Python 3.6 is likely not supported *at the system level*, since the system is supposed to be stable, thus sticking to the major.minor versions it started off with. If you want to install and use Python 3.6 (or 3.7 in a month's time or so), install it in `/usr/local`. –  Feb 08 '18 at 06:12

4 Answers4

11

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
K.S.
  • 2,846
  • 4
  • 24
  • 32
10

If you look at /usr/bin/add-apt-repository it says

#!/usr/bin/python3

at the top. If you updated with a newer python (e.g. dist had 3.5 but you installed 3.7) your /usr/bin/python3 points to a python that does not have apt_pkg.

You can temporarily edit /usr/bin/add-apt-repository to point to

#!/usr/bin/python3.5

(insert your distro python version)

laktak
  • 57,064
  • 17
  • 134
  • 164
6

I had a similar problem, when doing apt-get upgrade... The problem is that apt-get was trying to use python2.7, but the symlink was pointing to python3.4:

debian:/usr/bin# cat /etc/debian_version
8.10
debian:/usr/bin# ll /usr/bin/python
lrwxrwxrwx 1 root root 18 Feb 26 17:02 /usr/bin/python -> /usr/bin/python3.4

Fixed it by creating a new symlink

debian:/usr/bin# rm /usr/bin/python
debian:/usr/bin# ln -s /usr/bin/python2.7 /usr/bin/python
ptoxic
  • 141
  • 1
  • 2
  • 4
    I was able to fix a similar problem with `apt-add-repository`, only this time I had to relink my `python3` to point to `python3.5` instead of 3.6. Some concept though. – Philip Jun 06 '18 at 07:05
-3

Install python-dev module for specific version of python:

sudo apt install python3.6-dev
user1941407
  • 2,722
  • 4
  • 27
  • 39