90

I'm running code sudo apt update and fetch error

ModuleNotFoundError: No module named 'apt_pkg'.

Please, help me solve it.

sudo apt update


Hit:20 http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu bionic InRelease       
Fetched 93,3 kB in 2s (42,4 kB/s)                   
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
3

10 Answers 10

90

In my case the problem was due to upgrading python version from 3.6 to 3.8.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

sudo update-alternatives --config python3

Solved by:

Settings back the python version to 3.6

7
  • 3
    I did the same thing. Thanks, this solved my issue!
    – dandeto
    Commented Jul 19, 2020 at 2:33
  • I had somehow created a link from python3 to python3.6. I ran the first command in this post and it did work for me Commented Oct 24, 2020 at 21:04
  • 7
    What if I want to use the latest Python version by default? Commented Aug 13, 2021 at 20:12
  • 4
    @HeathMitchell you can edit the file /usr/bin/add-apt-repository and changing out the line #!/usr/bin/python3 with the specific version of python. For exemple: #!/usr/bin/python3.6. See more: askubuntu.com/a/1184982 Commented Dec 27, 2021 at 18:04
  • In Ubuntu 20.04, python3.8 should be set with the highest priority to make it work.
    – sudar
    Commented Mar 7, 2023 at 8:08
80

Just reinstalled python3-apt and the error disappeared

sudo apt remove python3-apt

restart and then install,

sudo apt install python3-apt

NOTE: Use only if, python3 is a new installation dependency. Else it can screw existing python3 based setups on your system. Use of --reinstall is a safer bet though.

11
  • 37
    On Ubuntu 19.10, apt remove python3-apt removes 38 other packages as well. So probably not a wise thing to do.
    – akaihola
    Commented Feb 23, 2020 at 19:46
  • 6
    Uninstalling and reinstalling may temporarily work, but it can also break your apt package later down the line. This error could be caused by any of the answers posted below.
    – A Duv
    Commented May 6, 2020 at 21:49
  • 4
    That's a horrible idea and a potentially dangerous thing to do as it removes a ton of other packages related to ubuntu, xserver and so on
    – Ælex
    Commented Sep 29, 2020 at 9:05
  • 13
    instead of remove,/install just do: apt install --reinstall python3-apt
    – fcm
    Commented Nov 18, 2020 at 21:16
  • 4
    I just did this without reading the other comments and it created different issues which I don't know how to solve. Commented May 29, 2021 at 11:10
35

Similar to some, I have python3.8 and python3.11 where python and python3 both are symlinks to python3.11 in /usr/bin

I ran the following which fixed the issue:

cd /usr/lib/python3/dist-packages/
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so
1
  • thx a lot. in my case it was sudo cp apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so
    – kuhajeyan
    Commented Jun 3 at 7:23
22

In my case, the problem was that I removed original /usr/bin/python3 symlink on Ubuntu 18.04 and replaced it with one pointing to python3.8. Problem disappeared when I restored the original pointing to python3.6

My takeaway is: if you need custom version of some library or module, install it in an isolated environment, do not mess up with system settings. Otherwise you are at risk of breaking something which can be noticed only later when it's difficult to figure out what exactly is wrong.

4
  • 3
    I did the same, the problem is there are blogs that show how to install a new version of python, which unfortunately will break system wide configurations.
    – Ælex
    Commented Sep 29, 2020 at 9:13
  • How did you restore the original?
    – pookie
    Commented Oct 25, 2021 at 16:32
  • 1
    @pookie cd /usr/bin && mv python3 python3_old && ln -s python3.8 python3 Commented Oct 27, 2021 at 7:49
  • Note that it also breaks if you use the recommended sudo update-alternatives method as well. For apt (and for gnome-terminal) it was simply better to leave the default python version as is and set up a virtual environment.
    – icc97
    Commented Jul 8 at 18:05
6

You need change the Python version (3.6 works for me). But, if you want another python version by default (3.8 for example):

Try editing the file /usr/bin/add-apt-repository and changing out the line #!/usr/bin/python3 with the specific version of python (like 3.6).

To change this file, open a text editor (like nano):

sudo nano /usr/bin/add-apt-repository

Replace #!/usr/bin/python3 for your python version (3.6 in this case)

#!/usr/bin/python3.6

Save the file.

Should work.

5

I had this issue, it was caused because I had installed python 3.6.9 with 3.5.2 (Ubuntu 16.04) I had also used sudo update-alternatives --config python3 to not use auto setting it back to auto solved the problem for me (and then giving 3.6.9 priority was the next step to get the newest version of python running by default) New user: I can't reply to other comments :(

5

I also got this error when running any command, it shows that the file in

/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py

couldn't import apt_pkg

there could be several reasons that cause this prob., but I'd suggest not directly removing and reinstalling python3-apt, could be problematic, the first try could be to check if you have updated the default python3 to a higher version before, like by me, i've updated the default python3 to python3.10, and that's why this error comes up. Run

sudo update-alternatives --config python3

to rechoose the default, by selecting the old one(python3.8 by me), the "no module named apt_pkg" error has been eliminated.

Btw, a tip also a memo for myself, next time when someone wanna run the process on a new python environment, it could be very safe and nice to just create a virtual env using conda, with no conflict with the current OS(Ubuntu 20.04 by me for example).

1
  • python3 --version seems has nothing to do with the default python3 version (sudo update-alternatives --condig python3), cuz my version still remains python 3.9.12, no matter set python3.10 or python3.8 as default
    – Frostson
    Commented Dec 24, 2022 at 13:42
4

The issue is caused by setting python3.7 or higher as default python3. @bluevalien like you said i had to use sudo update-alternatives --config pyhton3 and i set python3.6 as default. Sorry i cannot comment since i'm a new user too

1
0

I also had the issue with "cp: cannot stat 'apt_pkg.cpython-36m-x86_64-linux-gnu.so': No such file or directory while I was as super user. I did the same command withouth the sudo command and it worked fine.

I have in my system python 3.5, 3.6 and 3.7, so I do not think the version of python is a problem.

cp apt_pkg.cpython-36m-x86-64-linux-gnu.so.so apt_pkg.so

Regards.

0

Sometimes google search is better than chatgpt-4o. The solution is simple, just as this Q&A showed

sudo apt remove python3-apt
sudo apt install python3-apt

chatgpt-4o gave me wrong direction, and wrong answer. And I cost some time with its wrong solution.

1
  • Please do not repeat a previous answer to confirm that it works. Instead, simply upvote the answer the helped you.
    – L Tyrone
    Commented Jul 28 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.