How to Fix ModuleNotFoundError: no module named ‘apt_pkg’

Diagram of How to Fix ModuleNotFoundError: no module named 'apt_pkg'

Diagram

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.

  1. Install python-apt package.
  2. Reinstall the python-apt package.
  3. 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 thesudo 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!

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.