How to fix 'ModuleNotFoundError: No module named 'apt_pkg'?

给你一囗甜甜゛ 提交于 2020-07-18 04:46:11

问题


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

回答1:


Just reinstalled python3-apt and the error disappeared

sudo apt remove python3-apt

restart and then install,

sudo apt install python3-apt



回答2:


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




回答3:


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:


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



来源:https://stackoverflow.com/questions/56218562/how-to-fix-modulenotfounderror-no-module-named-apt-pkg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!