python-dev installation error: ImportError: No module named apt_pkg
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.
Belphegor
Updated on January 25, 2022Comments
-
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 over 11 yearsIt 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 over 11 yearsremove it using
apt-get remove --purge python-apt
and install it again -
Al R. over 11 yearsBe 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 over 11 yearsNot really sure if it was due to python-apt. Look over at packages.ubuntu.com/lucid/ubuntu-desktop for dependencies to ubuntu-desktop.
-
alvas about 8 years@Arnestig REMOVING
python-apt
sounds rather dangerous!! Learnt my lesson once and I shall not try it. -
Pamungkas Jayuda about 6 yearsremove python3 is bad idea :( if you want link to python 3.4 use : ln -s /usr/bin/python3.4 /usr/bin/python3
-
desaiankitb about 6 years
sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so
should be changed tosudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so apt_pkg.so
-
Alex Gurrola almost 6 yearsYou 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 almost 6 yearsI needed to run
sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so apt
-
Jamie Lindsey about 5 yearsIt'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 about 5 yearsTry to be more specific and explain what these two commands are doing.
-
abulka about 5 yearsAfter 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 runcd /usr/lib/python3/dist-packages
thensudo ln -s apt_pkg.cpython-{36m,37m}-x86_64-linux-gnu.so
thensudo apt install python3-pip
. -
StatguyUser about 5 yearsfor python 3.6, command will be
sudo ln -s apt_pkg.cpython-{35m,36m}-x86_64-linux-gnu.so
-
CloseISQ about 5 yearsRunning 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 about 5 yearsOn 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 about 5 yearshow do you downgrade it?
-
vineeshvs over 4 years@AlexGurrola Verified in Ubuntu 16.04 on 22Aug2019
-
Abhimanyu over 4 yearsCompiling python worked for me, here is the link github.com/pypa/pip/issues/5367#issue-320060428
-
Leonardo Isso over 4 yearsThis 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 ofapt_pkg.cpython-34m-i386-linux-gnu.so
orapt_pkg.cpython-3m-i386-linux-gnu.so
, but I hadapt_pkg.cpython-36m-i386-linux-gnu.so
... I copied this file toapt_pkg.so
and worked perfectly! Thanks! -
AnthonyD973 over 4 yearsJust wanted to confirm to others that this indeed worked for me.
-
David Medinets over 4 yearswhy is the downgrade the safest actin?
-
CpILL over 4 yearsFixed that error, now I'm getting
ImportError: cannot import name '_gi' from 'gi' (/usr/lib/python3/dist-packages/gi/__init__.py)
:(.. -
loretoparisi over 4 yearsthat's crazy, thank you! When in docker:
RUN ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
-
w3Develops over 4 yearsThis 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 over 4 yearsThis worked for me with python3.6 but when I revert to python3.7, the error is still there
-
jumbot over 4 yearsThis fixed my problem. This solves the problem if it was caused by switching the Python version from 3.6 to 3.7.
-
Scipio over 4 yearsThanks, 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 whatapt_pkg.so
is. -
Boris Verkhovskiy over 4 years@Arnestig thank you,
--purge
and then reinstalling seems to have fixed it for me -
Daniel about 4 yearsWorked, 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 about 4 yearsthis is the solution! After upgrading to python 3.7.
-
pauljohn32 about 4 yearsThis 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 about 4 years18.04.04, upgrading to python 3.7 and this worked for me as well! Thanks!!
-
Henry Palacios almost 4 yearsThis worked for me after upgrade python 3.8 on ubuntu18.04.
-
allyourcode almost 4 yearsWhat does the ln command do? I have never seen braces used that way before. Looks like some kind of black magic.
-
Sergey Shubin almost 4 yearsIt 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 almost 4 yearsthis simply works for me, thanks! One thing is before that we need to cd /usr/lib/python3/dist-packages
-
Carlos A. Ibarra over 3 yearsThose 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 over 3 yearsWorks for python 3.8 also !
-
Hassan Ajaz over 3 yearsvery helpful for new python users.
-
fcm over 3 yearsNot working after upgrade from 3.5 to 3.9.
-
Tanishq Vyas about 3 yearsThanks a lot, it worked for me for python3.7
-
XavierStuvw about 3 yearsThis 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 almost 3 yearsat least reinstall is safer...
sudo apt install --reinstall python3
-
Andre Leon Rangel almost 3 yearsplease add mode details to this answer. Did it work for you? what is this trying to achieve?
-
Andre Leon Rangel almost 3 yearsplease provide an answer here. You could copy and paste the commands and keep the link
-
Sylvain over 2 yearsfor me no 'm', just the python version number
apt_pkg.cpython-38-x86_64-linux-gnu.so
. -
Feline over 2 yearsThe accepted solution didn't work, but this one did.
-
Levin over 2 yearsThis works for me
-
siddhusingh over 2 yearsworks for me as well
-
Dan Nissenbaum about 2 yearsThis one worked for me on Ubuntu 20.02 after upgrading the default Python to Python 3.9
-
Carlos Carvalheira about 2 yearsSame Reason here !! Thanks