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

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


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 1sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2sudo update-alternatives --config python3

Solved by:

Settings back the python version to 3.6


Just reinstalled python3-apt and the error disappeared

sudo apt remove python3-apt

restart and then install,

sudo apt install python3-apt


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.