Ubuntu Error: No module named ‘apt_pkg’ (Step-by-Step Tutorial)


Ubuntu often uses the following command to add PPA sources:

sudo add-apt-repository ppa:XXX

Error: no module named ‘apt’_ pkg’ 。

This problem has been bothering me for a long time. Every time I want to solve it, I have been busy on the Internet for a long time without finding a solution

today I found the solution


Step 1: sudo GEDIT/usr/bin/apt add repository

We will find that the so-called “apt add repository” command is actually a python script, and the top line reads: #/ usr/bin/python3

This is a python 3 script

Step 2: sudo LS – L/usr/bin/python3

Display/usr/bin/python3 – > python3.5

Explain that on my Ubuntu, python3 is linked to python3.5. That’s the problem with Python 3.5

Step 3:

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

ls apt_ pkg*

Display: apt_ pkg.cpython-34m-x86_ 64-linux-gnu.so

Note the word 34m, which means that only Python 3.4 can use this component safely! But our computer python3 is linked to python3.5

different Ubuntu versions do not necessarily display 34m, so you must check this file yourself. Then modify python3 to link to the corresponding version

At this point, the solution is very simple

Step 4:

sudo rm /usr/bin/python3

sudo ln – S/usr/bin/python3.4/usr/bin/python3 (according to the file name and version)

It’s done! Go and see if the apt add repository command can be used

The ultimate mystery

sudo apt-get remove –purge python-apt

sudo apt-get install python-apt -f

sudo find/-name “apt_ pkg.cpython-35m-x86_ 64-linux-gnu.so”

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

sudo cp apt_ pkg.cpython-35m-x86_ 64-linux-gnu.so apt_ pkg.cpython-36m-x86_ 64-linux-gnu.so (depending on the file name version)

Similar Posts: