ModuleNotFoundError: No module named ‘apt_pkg’ error occurs when “python-apt package is not installed or is incompatible.“
How to fix the error?
Here are three ways to fix the ModuleNotFoundError: No module named ‘apt_pkg’ error.
- Install python-apt package.
- Reinstall the python-apt package.
- Set the correct location of the package.
Solution 1: Install the python-apt package
The easy way to fix this is by installing the python-apt package using the below command.
sudo apt-get install python-apt
It will ask for admin access; you must provide one to install the python-apt package.
Solution 2: Reinstall the python-apt package
You can reinstall the python-apt package using the “sudo apt install –reinstall python3-apt” command in your terminal.
If you still face the issue, try running the installation command with the –fix-missing flag.
sudo apt install python3-apt --fix-missing
If the error persists, try running the command with the –fix-broken flag.
sudo apt install python3-apt --fix-broken
You can also reinstall python3-apt using these series of commands:
sudo apt remove python3-apt sudo apt autoremove sudo apt autoclean sudo apt install python3-apt
Solution 3: Set the correct location of the package
The extension of the python-apt package is the “.so” file type.
To set the file at the exact location, use the below command.
/usr/lib/python3/dist-packages# cp apt_pkg.cpython-34m-i386-linux-gnu.so apt_pkg.so
This will fix the error you are having!