How to Solve Python ModuleNotFoundError: no module named ‘pip’

by | Programming, Python, Tips

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.


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:

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!