python-dev installation error: ImportError: No module named apt_pkg

PythonLinuxInstallationDebian

Python Problem Overview


I am Debian user, and I want to install python-dev, but when I run the code in the shell as a root:

# aptitude install python-dev

I get the following error:

Traceback (most recent call last):       
  File "/usr/bin/apt-listchanges", line 28, in <module>
    import apt_pkg
ImportError: No module named apt_pkg

What seems to be the problem and how can I resolve it?

Python Solutions


Solution 1 - Python

I met this problem when doing sudo apt-get update. My env is debian8, with python2.7 + 3.4(default) + 3.5.

The following code will only re-create a apt_pkg....so file for python 3.5

sudo apt-get install python3-apt --reinstall

The following code solved my problem,

cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-{your-version-number}-x86_64-linux-gnu.so apt_pkg.so

Replace {your-version-number} appropriately.

CAUTION, the following will create a symlink from apt_pkg37m to apt_pkg36m. make sure you are linking to the correct, or at least to an existing version by ll apt_pkg.cpython-*, and see which one(s) you have installed.

sudo ln -s apt_pkg.cpython-{36m,37m}-x86_64-linux-gnu.so

So, obviously, python3-apt checks the highest python version, instead of the current python version in use.

Solution 2 - Python

Solve it by this:

cd /usr/lib/python3/dist-packages
cp apt_pkg.cpython-34m-i386-linux-gnu.so apt_pkg.so

Or:

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

Basically, if you get a No such file or directory just ls to try to get the right name.

Solution 3 - Python

This happened to me on Ubuntu 18.04.2 after I tried to install Python3.7 from the deadsnakes repo.

Solution was this

  1. cd /usr/lib/python3/dist-packages/

  2. sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Solution 4 - Python

Make sure you have a working python-apt package. You could try and remove and install that package again to fix the problem with apt_pkg.so not being located.

apt-get install python-apt

Solution 5 - Python

This error will often occur when a newer version of python has been installed alongside an older version e.g;

  • Ubuntu 18.04.1 ships with python version 3.6.6

  • Installed ppa:deadsnakes/python3.7.1 or alternative

  • Run a command that uses the apt_pkg module and get an error such as;

         from CommandNotFound.db.db import SqliteDatabase
     File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
         import apt_pkg
    

When we install a non-distro python3 version with apt it will set a shared module directory to be that of python3 most usually it will be /usr/lib/python3.

Most of the time this will be ok, but under some circumstances the different versions of python rely on different libraries or shared objects/libraries than the other python version does, so as other answers have pointed out we need to link the .SO to the correct python version. So if we have python3.6 installed on a 64bit system then the apt_pkg .SO link would be

sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

But the problem lies in the fact that when we install a newer python version the link will update to point to the newest python version, which leads to the error of apt_pkg module not being found. By checking which version of python ships with your distro you can create the link as shown above. Or we use a method to offer the command a choice of python versions to link the .SO such as;

sudo ln -s apt_pkg.cpython-{36m,35m,34m}-x86_64-linux-gnu.so apt_pkg.so

Because python will create this link to the newest installed python version we give the command the option to choose from 3 python versions, of which it will choose the highest version given.

Solution 6 - Python

This worked for me on after updating python3.7 on ubuntu18.04

cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Solution 7 - Python

The solution of @user8178061 worked well but I did it with some modifications for my version wich is python3.7 with Ubuntu

I replaced the apt_pkg.cpython-3m-i386-linux-gnu.so with apt_pkg.cpython-36m-x86_64-linux-gnu.so

Here the two commands to execute:

cd /usr/lib/python3/dist-packages

sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Solution 8 - Python

I see everyone saying how to fix it with strange copying etc, but no one really said why the problem occurs.

So let me explain, for those of you who like me don't want to mess with system files only because someone on SO told them so.


The problem is that:

  • many system scripts have python3 shebang hardcoded into them. You can check it yourself:
~$ grep -R "\#\!/usr/bin/python3" /usr/lib/*

/usr/lib/cnf-update-db:#!/usr/bin/python3
/usr/lib/command-not-found:#!/usr/bin/python3
/usr/lib/cups/filter/pstotiff:#!/usr/bin/python3
/usr/lib/cups/filter/rastertosag-gdi:#!/usr/bin/python3 -u
grep: /usr/lib/cups/backend/cups-brf: Permission denied
/usr/lib/cups/backend/hpfax:#!/usr/bin/python3
/usr/lib/language-selector/ls-dbus-backend:#!/usr/bin/python3
/usr/lib/python3/dist-packages/language_support_pkgs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/softwareproperties/MirrorTest.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/installdriver.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/openprinting.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/xmldriverprefs.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/smburi.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/ppds.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/cupshelpers/debug.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/DistUpgrade/dist-upgrade.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/CommandNotFound/db/db.py:#!/usr/bin/python3
/usr/lib/python3/dist-packages/Quirks/quirkreader.py:#!/usr/bin/python3
grep: /usr/lib/ssl/private: Permission denied
/usr/lib/system-service/system-service-d:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release-gtk:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/do-partial-upgrade:#!/usr/bin/python3
/usr/lib/ubuntu-release-upgrader/check-new-release:#!/usr/bin/python3
/usr/lib/update-notifier/package-data-downloader:#!/usr/bin/python3
/usr/lib/update-notifier/backend_helper.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt_check.py:#!/usr/bin/python3
/usr/lib/update-notifier/apt-check:#!/usr/bin/python3

  • python apt package python-apt/python3-apt is a system package, so it's for default system python

Thus, the scripts will always get the version currently linked to python3, but fail because the apt package is not present.


General solution: NEVER change default python3 link. Ever. This also applies to python link - if an app was written in Python2 with some old syntax elements that don't work in Python3, the app will not work.

[My terminal broke that way because I use Terminator, which is apparently written in Python2.7 not compatible with Python3.]


Solutions presented here either suggest copying/linking the apt package files or changing python3 link.

Let's analyse both:

  1. Copying/linking the apt package

This shouldn't be a problem because from around Python3.4 all python scripts work on newer versions as well.

So far. But it may break in the future - if you keep your system long enough.

  1. Changing python3 link back

This is a great solution because we can get back to "never ever changing the link"


"But I like having to type just python!" - I like it too! That's how I got to this problem in the first place!

  1. In general, you should avoid manually changing system links - use update-alternatives instead to link different versions. This applies to any app with many versions. This will still break those system scripts (because it does change the link), but you can switch back and forth easily, without worrying whether you put link and dest in the right order or made a typo.

  2. Consider using other name than python/python3 for your link or alias.

  3. Or add your own python/python3 link to PATH (just like virtual environments do), without changing system links.

Solution 9 - Python

  1. Check your default Python 3 version:

    python3 --version
    Python 3.7.5
    
  2. cd into /usr/lib/python3/dist-packages and check the apt_pkg.* files. You will find that there is none for your default Python version:

    ll apt_pkg.*
    apt_pkg.cpython-36m-x86_64-linux-gnu.so
    
  3. Create the symlink:

    sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64-linux-gnu.so 
    

Solution 10 - Python

For some reason my install was missing apt_pkg.so in the python3 dist-packages dir. (apt_pkg.cpython-33m-x86_64-linux-gnu.so was there?!) but and I had to make a symlink apt_pkg.so -> apt_pkg.cpython-33m-x86_64-linux-gnu.so in /usr/lib/python3/dist-packages

I'm not sure whether my upgrade was broken or why this was the case. It occured after trying to upgrade (precise->raring->quantal upgrade)

Solution 11 - Python

if you're using python 3.7 downgrade it to python 3.6 by updating Alternatives, This worked for me

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

sudo update-alternatives --config python3

Solution 12 - Python

A last resort is sudo cp /usr/lib/python3/dist-packages/apt_pkg.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so if the ln command is too much for you or somehow magically doesn't work.

cp above can also be mv if you are only dedicated to using one Python version.

Solution 13 - Python

I'm on Ubuntu 16.04, and upgraded to Python 3.7. Here is the error that I had when trying to add a PPA

    sudo add-apt-repository ppa:ubuntu-toolchain-r/test                                           
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'

I was able to fix this error by making symbolic link with my initial python 3.4 apt_pkg.cpython-34m-x86_64-linux-gnu.so by creating the following symbolic link

sudo ln -s apt_pkg.cpython-34m-x86_64-linux-gnu.so apt_pkg.so

Solution 14 - Python

If you're using python 3.5, downgrade to 3.4. That's the safest move to do.

Under /usr/lib/python3/dist-packages you'll see *34m* which python 3.5 can't use. zhazha answer symlink to it.

Solution 15 - Python

In addition to making a symbolic link for apt_pkg.so, you may want to make apt_inst.so in the same manner of apt_pkg.so.

ln -s apt_inst.cpython-35m-x86_64-linux-gnu.so apt_inst.so 

Solution 16 - Python

I tried to create the link but many other problems happened. So, you can select the old version of python to install things using:

sudo update-alternatives --config python3

And return to the desirable version right after using the same command. Hope it works.

Solution 17 - Python

Windows 10 WSL v1 (Ubuntu 16.04.6 LTS)

This reddit answer (slightly modified worked for me)

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

Solution 18 - Python

After spending 4 hours I have got this solution which finally worked for me I hope this will help...

It is important to understand that sometimes when you upgrade from an older python version some packages stay in the previous version path, so here is what I did:

cd /usr/lib/python3/dist-packages

Check the existence of a file named apt_pkg.cpython-35m-x86_64-linux-gnu.so or 34m or 36m listing the files and when you find it, delete the current apt_pkg.so file in

/usr/lib/python3/dist-packages

Finally create a link with the correct path using apt_pkg.so like this:

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

Now you can try again and It should work.

Solution 19 - Python

This variant work for me.

cd  /usr/lib/python3/dist-packages
ls -la /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

If you get an error message saying too many levels of symbolic links as shown below:

cp: failed to access '/usr/lib/python3/dist-packages/apt_pkg.so': Too many levels of symbolic links

Then you need to simply unlink the apt_pkg.so file. Use the following command:

sudo unlink apt_pkg.so

And then use the command

sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

Good luck!

Original answer: https://askubuntu.com/a/1227625

Solution 20 - Python

None of the answers worked for me (I am using Ubuntu 16.04 and Python 3.6). So I finally solved the issue as following:

1- connect to the FTP of the server

2- go to the folder "/usr/lib/python3/dist-packages/"

3- duplicate the file "apt_pkg.cpython-35m-x86_64-linux-gnu.so"

4- rename this duplicated file to "apt_pkg.cpython-36m-x86_64-linux-gnu.so"

That's it!

Solution 21 - Python

Well, the cleanest solution for me is to rebuild the related library with your new Python version, in its specific space.

I'm doing this right now, switching from Python3.9 to Python3.10.1.

So:

> cd /tmp/ > apt source python3-apt > cd python-apt-version-ecc > python3-new-version setup.py build > python3-new-version setup.py install

Done this also with dependencies packages (software-properties, python-launchpadlib, and so on), and now add-apt-repository works like a charm.

Which ones to recompile? Simply watch out the complains of running add-apt-repository.

By the way, I'm on Ubuntu 21.04, but the logic is the same on any Debian like system.

Solution 22 - Python

Please review the following documentation.

It could help you to solve the problem.

Solve the problem of replacing the python version with ModuleNotFoundError: No module named 'apt_pkg'

Solution 23 - Python

Please try to fix this by setting the locale variables:

export LC_ALL="en_US.UTF-8"

export LC_CTYPE="en_US.UTF-8"

Solution 24 - Python

Just in case it helps another, I finally solved this problem, that was apparently caused by python version conflicts, by redirecting the link python3, then redirecting it to the right python version:

sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.4

You may need to enter the correct python version, found with:

python3 -V

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBelphegorView Question on Stackoverflow
Solution 1 - PythonzhazhaView Answer on Stackoverflow
Solution 2 - Pythonuser8178061View Answer on Stackoverflow
Solution 3 - PythonMirror MirageView Answer on Stackoverflow
Solution 4 - PythonArnestigView Answer on Stackoverflow
Solution 5 - PythonJamie LindseyView Answer on Stackoverflow
Solution 6 - PythonasthaView Answer on Stackoverflow
Solution 7 - PythonHugo SohmView Answer on Stackoverflow
Solution 8 - Pythonh4z3View Answer on Stackoverflow
Solution 9 - PythonNico SchlömerView Answer on Stackoverflow
Solution 10 - PythonJamie PateView Answer on Stackoverflow
Solution 11 - PythonViren PatelView Answer on Stackoverflow
Solution 12 - PythonTong NiuView Answer on Stackoverflow
Solution 13 - PythonGigaWattsView Answer on Stackoverflow
Solution 14 - PythonPobeView Answer on Stackoverflow
Solution 15 - PythonJihwan ParkView Answer on Stackoverflow
Solution 16 - PythonAlexandre CrivellaroView Answer on Stackoverflow
Solution 17 - PythonLeo Furze-WaddockView Answer on Stackoverflow
Solution 18 - PythonAahadView Answer on Stackoverflow
Solution 19 - PythonAndrew LutsyukView Answer on Stackoverflow
Solution 20 - PythonJulio S.View Answer on Stackoverflow
Solution 21 - PythonAndrea GelminiView Answer on Stackoverflow
Solution 22 - PythonOniView Answer on Stackoverflow
Solution 23 - Pythonrajendra sharmaView Answer on Stackoverflow
Solution 24 - Pythonmatthias2tView Answer on Stackoverflow