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

249,486

Solution 1

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 2

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 3

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 4

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 5

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.

Share:
249,486
Belphegor
Author by

Belphegor

Updated on January 25, 2022

Comments

  • Belphegor
    Belphegor over 2 years

    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?

  • Belphegor
    Belphegor over 11 years
    It is already installed. When I hit: # apt-get install python-apt it gives me Reading package lists... Done Building dependency tree Reading state information... Done python-apt is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. And when I try again with: # aptitude install python-dev It gives me the same error again. Some other ideas what should I do?
  • Arnestig
    Arnestig over 11 years
    remove it using apt-get remove --purge python-apt and install it again
  • Al R.
    Al R. over 11 years
    Be aware of other dependencies. I removed the package (too) quickly in Ubuntu and lots of other dependencies were removed as well (e.g. ubuntu-desktop). It's my fault for not paying attention to the notes in the log, however.
  • Arnestig
    Arnestig over 11 years
    Not really sure if it was due to python-apt. Look over at packages.ubuntu.com/lucid/ubuntu-desktop for dependencies to ubuntu-desktop.
  • alvas
    alvas about 8 years
    @Arnestig REMOVING python-apt sounds rather dangerous!! Learnt my lesson once and I shall not try it.
  • Pamungkas Jayuda
    Pamungkas Jayuda about 6 years
    remove python3 is bad idea :( if you want link to python 3.4 use : ln -s /usr/bin/python3.4 /usr/bin/python3
  • desaiankitb
    desaiankitb about 6 years
    sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so should be changed to sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so apt_pkg.so
  • Alex Gurrola
    Alex Gurrola almost 6 years
    You are amazing! For me, it was sudo ln -s apt_pkg.cpython-{35m,36m}-x86_64-linux-gnu.so for python3.6, and this horrific bug is now gone.
  • Philippe C
    Philippe C almost 6 years
    I needed to run sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so apt
  • Jamie Lindsey
    Jamie Lindsey about 5 years
    It's never a good idea to tell people to remove things from there system unless it is really needed. Luckily /usr/bin/python3 is normally just a symbolic link anyway. I really think you should just delete this answer.
  • dpapadopoulos
    dpapadopoulos about 5 years
    Try to be more specific and explain what these two commands are doing.
  • abulka
    abulka about 5 years
    After installing Python 3.7 next to the default 3.6 in Ubuntu 18.04 with sudo apt install python3.7 I got this apt_pkg error trying to run pip, so I needed to run cd /usr/lib/python3/dist-packages then sudo ln -s apt_pkg.cpython-{36m,37m}-x86_64-linux-gnu.so then sudo apt install python3-pip.
  • StatguyUser
    StatguyUser about 5 years
    for python 3.6, command will be sudo ln -s apt_pkg.cpython-{35m,36m}-x86_64-linux-gnu.so
  • CloseISQ
    CloseISQ about 5 years
    Running 18.04.2, your first recommendation worked for me and I could finally run the sudo apt-get update with no errors. Your second recommendation return an error saying apt-get.so was not a folder.
  • Naren Yellavula
    Naren Yellavula about 5 years
    On Ubuntu 18.04, use this $ cd /usr/lib/python3/dist-packages $ sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
  • Pynchia
    Pynchia about 5 years
    how do you downgrade it?
  • vineeshvs
    vineeshvs over 4 years
    @AlexGurrola Verified in Ubuntu 16.04 on 22Aug2019
  • Abhimanyu
    Abhimanyu over 4 years
    Compiling python worked for me, here is the link github.com/pypa/pip/issues/5367#issue-320060428
  • Leonardo Isso
    Leonardo Isso over 4 years
    This worked for me as well... I listed all the files on the /usr/lib/python3/dist-packages, and I saw that I didn't have of apt_pkg.cpython-34m-i386-linux-gnu.so or apt_pkg.cpython-3m-i386-linux-gnu.so, but I had apt_pkg.cpython-36m-i386-linux-gnu.so... I copied this file to apt_pkg.so and worked perfectly! Thanks!
  • AnthonyD973
    AnthonyD973 over 4 years
    Just wanted to confirm to others that this indeed worked for me.
  • David Medinets
    David Medinets over 4 years
    why is the downgrade the safest actin?
  • CpILL
    CpILL over 4 years
    Fixed that error, now I'm getting ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-packages/gi/__init__.py) :(..
  • loretoparisi
    loretoparisi over 4 years
    that's crazy, thank you! When in docker: RUN ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-li‌​nux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
  • w3Develops
    w3Develops over 4 years
    This worked for me as well. As @LeonardoIsso did I also listed all files to find the proper file name because i am on 32 bit Linux.
  • Hugo Sohm
    Hugo Sohm over 4 years
    This worked for me with python3.6 but when I revert to python3.7, the error is still there
  • jumbot
    jumbot over 4 years
    This fixed my problem. This solves the problem if it was caused by switching the Python version from 3.6 to 3.7.
  • Scipio
    Scipio over 4 years
    Thanks, had the same issue! Consider using sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so so it's clear what apt_pkg.so is.
  • Boris Verkhovskiy
    Boris Verkhovskiy over 4 years
    @Arnestig thank you, --purge and then reinstalling seems to have fixed it for me
  • Daniel
    Daniel about 4 years
    Worked, but I had to change it to sudo cp apt_pkg.cpython-37m-x86_64-linux-gnu.so apt_pkg.so for some reason
  • JOHN
    JOHN about 4 years
    this is the solution! After upgrading to python 3.7.
  • pauljohn32
    pauljohn32 about 4 years
    This was correct fix for me. I caused this problem by using pip3 as root, even though I knew it was a bad idea. I wanted to see how badly I'd get shocked if I put my finger in a light socket. Answer is: pretty bad. Also messing with the /etc/alternatives setup is not for the feint of heart. Not for anybody who is a part time visitor. I think crossing what python link pointed at which python was problem that started me down path to disaster.
  • RJaus
    RJaus about 4 years
    18.04.04, upgrading to python 3.7 and this worked for me as well! Thanks!!
  • Henry Palacios
    Henry Palacios almost 4 years
    This worked for me after upgrade python 3.8 on ubuntu18.04.
  • allyourcode
    allyourcode almost 4 years
    What does the ln command do? I have never seen braces used that way before. Looks like some kind of black magic.
  • Sergey Shubin
    Sergey Shubin almost 4 years
    It looks like this solution was already proposed in at least 6 another answers to this question (and 3 more just suggested to copy the file instead of symlinking).
  • Hao Yellow
    Hao Yellow almost 4 years
    this simply works for me, thanks! One thing is before that we need to cd /usr/lib/python3/dist-packages
  • Carlos A. Ibarra
    Carlos A. Ibarra over 3 years
    Those braces are not a property of the the ln command. They invoke shell brace expansion, see gnu.org/software/bash/manual/html_node/Brace-Expansion.html
  • user2080866
    user2080866 over 3 years
    Works for python 3.8 also !
  • Hassan Ajaz
    Hassan Ajaz over 3 years
    very helpful for new python users.
  • fcm
    fcm over 3 years
    Not working after upgrade from 3.5 to 3.9.
  • Tanishq Vyas
    Tanishq Vyas about 3 years
    Thanks a lot, it worked for me for python3.7
  • XavierStuvw
    XavierStuvw about 3 years
    This worked for me with Python 3.8.5 with Ubuntu 20.04; the cause for trouble was having played around with the targets py2/py3 of the symbolic link /bin/usr/python. This confused some programs calling py3 which threw the OP's error. The other answer stackoverflow.com/a/64241654/5459638 contains useful information to understand what happened.
  • Andre Leon Rangel
    Andre Leon Rangel almost 3 years
    at least reinstall is safer... sudo apt install --reinstall python3
  • Andre Leon Rangel
    Andre Leon Rangel almost 3 years
    please add mode details to this answer. Did it work for you? what is this trying to achieve?
  • Andre Leon Rangel
    Andre Leon Rangel almost 3 years
    please provide an answer here. You could copy and paste the commands and keep the link
  • Sylvain
    Sylvain over 2 years
    for me no 'm', just the python version number apt_pkg.cpython-38-x86_64-linux-gnu.so.
  • Feline
    Feline over 2 years
    The accepted solution didn't work, but this one did.
  • Levin
    Levin over 2 years
    This works for me
  • siddhusingh
    siddhusingh over 2 years
    works for me as well
  • Dan Nissenbaum
    Dan Nissenbaum about 2 years
    This one worked for me on Ubuntu 20.02 after upgrading the default Python to Python 3.9
  • Carlos Carvalheira
    Carlos Carvalheira about 2 years
    Same Reason here !! Thanks