46

I run below command on ubuntu 16.4 :

sudo add-apt-repository ppa:noobslab/apps

of below question:

How to install PlayOnLinux on Ubuntu 16.04

and get below 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'

Thanks for help

karel
  • 114,770

8 Answers8

76

Its very late but could be helpful for others.

> cd /usr/lib/python3/dist-packages
> sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
Gulzar
  • 210
saranjeet singh
  • 876
  • 7
  • 7
  • 26
    I adapted this answer to use a symlink instead and this seemed to work too: ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so – Gibbsoft Jul 29 '19 at 14:05
22

There was a similar question on 2014 (Problem with update-manager: No module named 'apt_pkg' in Ubuntu 13.10, having installed Python 3.4 on /usr/local/lib). The solution worked for me.

sudo apt-get remove python3-apt
sudo apt-get install python3-apt
20

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

edit /usr/bin/add-apt-repository where it says

#! /usr/bin/python3

to point to

#! /usr/bin/python3.5

(insert your distro python version, 3.5 for 16.04 or 3.6 for 18.04)

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • Checking the original python version for my Ubuntu version was key for me. – hrrrrrr5602 Jan 27 '22 at 15:47
  • I wouldn't recommend this; instead revert /usr/bin/python3 to what it was before. I'm pretty sure other system scripts use /usr/bin/python3 and apt_pkg, so this is only a bandage fix. – wjandrea Sep 14 '23 at 14:26
8

Create a symbolic link at apt_pkg.so which references to file apt_pkg.cpython-35m-x86_64-linux-gnu.so.

cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
karel
  • 114,770
4

This is the solution that worked for me for the latest apt_pkg_cpython package on Ubuntu 18.04:

cd /usr/lib/python3/dist-packages/
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
sudo apt-get update
wjandrea
  • 14,236
  • 4
  • 48
  • 98
3

The only thing that worked for me was to uninstall python 3.5 and then install the apt tools again:

sudo apt remove python3.5*
sudo apt install python3-apt
sudo apt install software-properties-common
2

The following worked for me. Firstly check the target apt_pkg library:

ls -l /usr/lib/python3/dist-packages/apt_pkg*

it should return the following:

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

where 35 stands for the python version. Set this version as default for python3:

sudo update-alternatives --set python3 /usr/bin/python3.5
dkolmakov
  • 121
2

To solve this remove the alternatives

sudo update-alternatives --remove-all python3

Then

sudo ln -sf /usr/bin/python3.6 /usr/bin/python3
wjandrea
  • 14,236
  • 4
  • 48
  • 98
sree
  • 101