33

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 '18 at 12:59
  • Thanks . how reinstalling ? Aug 26 '18 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 '18 at 13:02
  • Thanks . i run sudo apt install --reinstall python3-software-properties but Still the problem persists Aug 26 '18 at 13:07
  • 1
    Try to reinstall two other packages: sudo apt-get install --reinstall python-apt python3-apt.
    – N0rbert
    Aug 26 '18 at 14:11
50

Its very late but could be helpful for others.

 /usr/lib/python3/dist-packages# sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
3
  • 24
    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
  • 2
    use sudo in front Jul 13 '20 at 6:52
  • 3
    got an error bash: /usr/lib/python3/dist-packages#: No such file or directory Jul 14 '20 at 4:00
21

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

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 it says

#! /usr/bin/python3

Then edit /usr/bin/add-apt-repository to point to

#! /usr/bin/python3.5

(insert your distro python version 3.5 for 16.04 & 3.6 if 18.04)

1
  • Thanks this solved my error!
    – Laban
    Sep 10 '20 at 16:40
7

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
1
2

To solve this remove the alternatives

`sudo update-alternatives --remove-all python3`

Then

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

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
1

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
1

This is the solution that worked for me for the latest apt_pkg_cpython package on Ubuntu18.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

Not the answer you're looking for? Browse other questions tagged or ask your own question.