This error occurs when you try to use pip, but it is not installed in your Python environment. This can happen if you skip installing pip when installing Python or when creating a virtual environment, or after explicitly uninstalling pip.
You can solve this error by downloading pip using the following curl command
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then install pip by running:
python3 get-pip.py
If this does not work, you can use ensurepip to bootstrap the pip installer into an existing Pip installation or virtual environment. For example,
# Linux python3 -m ensurepip --upgrade # MacOS python3 -m ensurepip --upgrade # Windows py -m ensurepip --upgrade
This tutorial will go through the ways to ensure pip is installed in your environment.
Table of contents
Install pip by Downloading get-pip.py
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl
command allows you to specify a direct download link. Using the -o
option sets the name of the downloaded file.
Install pip by running:
python3 get-pip.py
Bootstrap pip using ensurepip
You can use ensurepip to bootstrap the pip installer into an existing Pip installation or virtual environment. For example,
# Linux python3 -m ensurepip --upgrade # MacOS python3 -m ensurepip --upgrade # Windows py -m ensurepip --upgrade
Install pip using Operating System Specific command
If the above solutions do not work, you can try to install pip using the command specific to your operating system.
Installing pip for Linux
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
Installing pip for Mac Operating System
You can install Python3 and pip3 using brew with the following command:
brew install python
Upgrading pip
You may also need to upgrade pip, which you can do with the following commands:
# Linux python3 -m pip install --upgrade pip # MacOS python3 -m pip install --upgrade pip # Windows py -m pip install --upgrade pip
Check pip and Python version
Ensure that the Python version in use matches the pip version. You can check versions from the command line using the --version
flag. For example,
python --version
Python 3.8.8
pip --version
pip 21.2.4 from /Users/Research/opt/anaconda3/lib/python3.8/site-packages/pip (python 3.8)
Note that the –version returns the version of Python is 3.8.8, and the pip installer in use is for 3.8.
Recreate Virtual Environment
If you are using a virtual environment and the error persists despite trying the above solutions, you can recreate the environment. For example,
# deactivate environment deactivate # remove the virtual environment folder rm -rf venv # Initial a new virtual environment python3 -m venv venv # Activate on Linux/MacOS source venv/bin/activate # Activate on Windows (cmd.exe) venv\Scripts\activate.bat # Activate on Windows (PowerShell) venv\Scripts\Activate.ps1
Summary
Congratulations on reading to the end of this tutorial.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
For further reading on missing modules in Python, go to the article:
- How to Solve ModuleNotFoundError: no module named ‘plotly’.
- How to Solve Python ModuleNotFoundError: no module named ‘pymongo’
- How to Solve Python ModuleNotFoundError: no module named ‘xgboost’
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.