66

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
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
Mogikan
  • 769
  • 1
  • 5
  • 3
  • 3
    Possible duplicate of [python-dev installation error: ImportError: No module named apt\_pkg](https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg) – knh190 May 20 '19 at 10:04
  • 2
    This is not a programming question and doesn't belong here. Please delete it and repost it on superuser.com – bruno desthuilliers May 20 '19 at 11:45
  • @bruno Agreed, but [ubuntu.se] would be a better fit – wjandrea Apr 18 '22 at 02:00

9 Answers9

70

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

nsola
  • 765
  • 6
  • 8
65

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.

parasrish
  • 3,864
  • 26
  • 32
Vijay
  • 839
  • 5
  • 5
  • 30
    On Ubuntu 19.10, `apt remove python3-apt` removes 38 other packages as well. So probably not a wise thing to do. – akaihola Feb 23 '20 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 May 06 '20 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 Sep 29 '20 at 09:05
  • 11
    instead of remove,/install just do: apt install --reinstall python3-apt – fcm Nov 18 '20 at 21:16
  • @fcm - this did the trick for me, thanks! – Ascalonian Dec 04 '20 at 14:35
  • 2
    I just did this without reading the other comments and it created different issues which I don't know how to solve. – Jeroen Vermunt May 29 '21 at 11:10
  • 3
    Indeed a terrible idea. Lots of packages got removed. I copied out the terminal messages and reinstalled all removed packages one-by-one. Hopefully, no links have been broken. It taught me a good lesson - read the message before type `y`! – Anthony Jul 29 '21 at 16:25
  • 1
    This is not a good method. Please use this command in caution. It removes more than it installs. Why didn't you put a warning!! – Meric Ozcan Oct 24 '21 at 09:23
  • 1
    it's just crashed a lot of features on my system. you should put a warning – victor israe Dec 16 '21 at 15:22
  • Do not remove this package. It is not the only package to be removed. Some other packages are removed too and that damaged my Ubuntu installation and some functionalities are not working. I had to install a new Ubuntu. – Ahmed Gad Oct 03 '22 at 16:55
20

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.

Fedorov7890
  • 1,173
  • 13
  • 28
11

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
Damo
  • 5,698
  • 3
  • 37
  • 55
5

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.

3

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 :(

bluevalien
  • 31
  • 2
2

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

amilone
  • 21
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30047215) – Emi OB Oct 11 '21 at 12:18
1

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).

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Frostson
  • 11
  • 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 Dec 24 '22 at 13:42
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.

Cyberfete
  • 11
  • 3