Last updated: Apr 10, 2024
Reading timeยท4 min
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.
apt_pkg
on UbuntuOpen your terminal and run the following command to install the apt_pkg
module.
sudo apt update sudo apt install --reinstall python3-apt
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.
python --version python3 --version
For example, my Python version is 3.10
, so I'd run the following command.
sudo apt update sudo apt install --reinstall python3.10-apt
If the suggestion didn't help, try running the installation command with the
--fix-missing
flag.
sudo apt install python3-apt --fix-missing # ๐๏ธ For a specific Python version sudo apt install python3.10-apt --fix-missing
If the error persists, try to run the command with the --fix-broken
flag.
sudo apt install python3-apt --fix-broken # ๐๏ธ For a specific Python version sudo apt install python3.10-apt --fix-broken
Run the following command from your terminal to cd
into the dist-packages
directory.
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.
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.
sudo ln -s apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so
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.
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.
If the error persists, copy the apt_pkg.cpython
file to apt_pkg
as follows.
The first command changes to the dist-packages
directory.
cd /usr/lib/python3/dist-packages cp apt_pkg.cpython-310-x86_64-linux-gnu.so apt_pkg.so
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.
cd /usr/lib/python3/dist-packages ls apt_pkg.cpython-*
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.
ls
The file names are ordered in ascending order, so the apt_pkg.cpython-XXX
file
should be near the top.
pip
If the error persists, run the following command.
# ๐๏ธ On Linux or macOS python -m ensurepip --upgrade # ๐๏ธ Using python 3 python3 -m ensurepip --upgrade # ๐๏ธ On Windows py -m ensurepip --upgrade
If you get the "ModuleNotFoundError: No module named 'pip'", check out my
other article with instructions on how to
install pip
.
python3-distutils
installedYou should also make sure that you have python3-distutils
installed as well.
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
deadsnakes
PPA insteadIf 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.
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update sudo apt-get install python3.10-distutils sudo apt-get install python3.10-apt
3.9.X
, you would issue the sudo apt-get install python3.9-distutils
command.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:
Open your terminal in the location where the get-pip.py
file is downloaded and
run the following command.
# ๐๏ธ On Linux or macOS python get-pip.py # ๐๏ธ Using python 3 python3 get-pip.py # ๐๏ธ On Windows py get-pip.py
The get-pip.py
script uses bootstrapping logic to install pip
.
You can also download the script using curl
.
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
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.