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?
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.
To understand why this is happening, see this answer further down: https://stackoverflow.com/a/64241654/21539
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.
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
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
This error will often occur when a newer version of python has been installed alongside an older version e.g;
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.
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
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:
~$ 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
/python3-apt
is a system package, so it's for default system pythonThus, 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:
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.
python3
link backThis 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!
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.
Consider using other name than python
/python3
for your link or alias.
Or add your own python
/python3
link to PATH (just like virtual environments do), without changing system links.
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
Check your default Python 3 version:
python3 --version
Python 3.7.5
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
Create the symlink:
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64-linux-gnu.so
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)
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
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.
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
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.
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
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.
In my case I ran below command and it fixed the error:
sudo apt install --reinstall python3 python python3-minimal --fix-broken
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With