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

10
  • 2
    Try reinstalling the python3-software-properties package. It provides the missing module.
    – user535733
    Aug 26, 2018 at 12:59
  • Thanks . how reinstalling ? Aug 26, 2018 at 13:00
  • If you don't know how to reinstall a package, then you may not be prepared to manage PPAs yet, so make sure you maintain good backups of your data. Open a terminal: sudo apt install --reinstall <package name>
    – user535733
    Aug 26, 2018 at 13:02
  • Thanks . i run sudo apt install --reinstall python3-software-properties but Still the problem persists Aug 26, 2018 at 13:07
  • 1
    Try to reinstall two other packages: sudo apt-get install --reinstall python-apt python3-apt.
    – N0rbert
    Aug 26, 2018 at 14:11

8 Answers 8

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
1
  • 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, 2019 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
8
  • 3
    Thanks . now I have bleow error : sudo: add-apt-repository: command not found Aug 28, 2018 at 12:02
  • 1
    Please do a sudo apt install software-properties-common after that, to get rid of the sudo: add-apt-repository: command not found error.
    – WaughWaugh
    Aug 12, 2019 at 7:54
  • 12
    Removing python3-apt is fairly dangerous since it already removes import packages like ubuntu-desktop and xorg. Oct 20, 2019 at 3:24
  • 4
    PLS heed the warning about the dangers of removing python3-apt. Nov 24, 2019 at 3:51
  • 1
    @DavidMedinets what should I do if I'm such a dumbass and did that? :facepalm:
    – M. Volf
    Dec 31, 2019 at 18:01
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)

2
  • Checking the original python version for my Ubuntu version was key for me. Jan 27, 2022 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, 2023 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
0
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
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
2

To solve this remove the alternatives

sudo update-alternatives --remove-all python3

Then

sudo ln -sf /usr/bin/python3.6 /usr/bin/python3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .