The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping

--

Chapters
00:00 Python-Dev Installation Error: Importerror: No Module Named Apt_pkg
00:28 Accepted Answer Score 77
00:46 Answer 2 Score 288
01:57 Answer 3 Score 201
02:15 Answer 4 Score 125
02:37 Thank you

--

Full question
https://stackoverflow.com/questions/1370...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #linux #installation #debian

#avk47



ANSWER 1

Score 288


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:

  1. Go to dist-packages
cd /usr/lib/python3/dist-packages
  1. Locate the appropriate .so file for the python version installed on your system:
ls -l | grep apt_pkg
  1. Create a symbolic link:
sudo ln -s apt_pkg.cpython-{your-version-number}-x86_64-linux-gnu.so apt_pkg.so
  1. 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 ls apt_pkg.cpython-*, and see which one(s) you have installed.

sudo ln -s apt_pkg.cpython-{35m,34m}-x86_64-linux-gnu.so apt_pkg.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




ANSWER 2

Score 201


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.




ANSWER 3

Score 125


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




ACCEPTED ANSWER

Score 77


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