ModuleNotFoundError: No module named 'apt_pkg' in Ubuntu

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# ModuleNotFoundError: No module named 'apt_pkg' in Ubuntu

The "ModuleNotFoundError: No module named 'apt_pkg'" error occurs when the apt_pkg library isn't installed for the specific Python installation.

To solve the error, run the sudo apt install --reinstall python3-apt command in your terminal.

modulenotfounderror no module named apt pkg

# Installing apt_pkg on Ubuntu

Open your terminal and run the following command to install the apt_pkg module.

shell
sudo apt update sudo apt install --reinstall python3-apt

install apt pkg on ubuntu

If the error persists, try to scope the command to your specific version of Python.

You can use the python --version command to get your version of Python.

shell
python --version python3 --version

get python version

For example, my Python version is 3.10, so I'd run the following command.

shell
sudo apt update sudo apt install --reinstall python3.10-apt

reinstall python 3 apt

If the suggestion didn't help, try running the installation command with the --fix-missing flag.

shell
sudo apt install python3-apt --fix-missing # ๐Ÿ‘‡๏ธ For a specific Python version sudo apt install python3.10-apt --fix-missing

install python3 apt fix missing

If the error persists, try to run the command with the --fix-broken flag.

shell
sudo apt install python3-apt --fix-broken # ๐Ÿ‘‡๏ธ For a specific Python version sudo apt install python3.10-apt --fix-broken

install python3 apt fix broken

# Linking to the correct apt_pkg file

Run the following command from your terminal to cd into the dist-packages directory.

main.py
cd /usr/lib/python3/dist-packages ls apt_pkg.cpython-*

The first command switches to the dist-packages directory.

The second command should print the name of the file you should use in the next command.

print name of apt pkg cpython file

The name depends on your Python version.

For example, my Python version is 3.10 which is reflected in the name of the file.

I have to run the following command to create a symbolic link.

shell
sudo ln -s apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so

create a symbolic link

Make sure to replace the apt_pkg.cpython-XXX file name with the filename for your Python version.

You can get the name by running one of the following commands.

main.py
ls apt_pkg.cpython-* ls -l | grep apt_pkg

The command will create a symbolic link to an apt_pkg.so file in your /usr/lib/python3/dist-packages directory.

# Copying the apt_pkg.cpython file to solve the error

If the error persists, copy the apt_pkg.cpython file to apt_pkg as follows.

The first command changes to the dist-packages directory.

main.py
cd /usr/lib/python3/dist-packages cp apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so

copy apt pkg file

The second command will be different and depends on your Python version.

You can start typing cp apt_pkg.cpython and press Tab to autocomplete.

My Python version is 3.10, so it is included in the file I have to copy to apt_pkg.so.

Run the following command to print the name of your apt_pkg.cpython file.

shell
cd /usr/lib/python3/dist-packages ls apt_pkg.cpython-*

print name of apt pkg cpython file

You can also use the ls command without a filter to print the contents of the directory and look for the apt_pkg.cpython-XXX file.

shell
ls

find apt pkg file

The file names are ordered in ascending order, so the apt_pkg.cpython-XXX file should be near the top.

# Upgrade your version of pip

If the error persists, run the following command.

shell
# ๐Ÿ‘‡๏ธ On Linux or macOS python -m ensurepip --upgrade # ๐Ÿ‘‡๏ธ Using python 3 python3 -m ensurepip --upgrade # ๐Ÿ‘‡๏ธ On Windows py -m ensurepip --upgrade

ensurepip upgrade

If you get the "ModuleNotFoundError: No module named 'pip'", check out my other article with instructions on how to install pip.

# Make sure you have python3-distutils installed

You should also make sure that you have python3-distutils installed as well.

shell
sudo apt update sudo apt install --reinstall python3-distutils # ๐Ÿ‘‡๏ธ For Python 3.10 (scoped to specific version) sudo apt install --reinstall python3.10-distutils # ๐Ÿ‘‡๏ธ For Python 3.11 sudo apt install --reinstall python3.11-distutils

install python3 distutils

# Use the deadsnakes PPA instead

If that didn't help, add the deadsnakes PPA and install a specific version of apt and distutils.

For example, my Python version is 3.10.4, so I would run the sudo apt-get install python3.10-distutils command.

shell
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update sudo apt-get install python3.10-distutils sudo apt-get install python3.10-apt
If your Python version is 3.9.X, you would issue the sudo apt-get install python3.9-distutils command.

# Using the official get-pip.py script

If the error persists, use the official get-pip script to install pip.

Download the script from https://bootstrap.pypa.io/get-pip.py by:

  1. Clicking on the link.
  2. Right-clicking and selecting "Save as" in your browser.

Open your terminal in the location where the get-pip.py file is downloaded and run the following command.

shell
# ๐Ÿ‘‡๏ธ On Linux or macOS python get-pip.py # ๐Ÿ‘‡๏ธ Using python 3 python3 get-pip.py # ๐Ÿ‘‡๏ธ On Windows py get-pip.py

python get pip py install

The get-pip.py script uses bootstrapping logic to install pip.

You can also download the script using curl.

shell
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py --force-reinstall python3 get-pip.py --force-reinstall py get-pip.py --force-reinstall

using get pip script to install pip

The --force-reinstall option forces pip to reinstall the package.

If you get any errors related to pip, install pip by following the instructions in my ModuleNotFoundError: No module named 'pip' in Python article.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev