ModuleNotFoundError: No module named 'pip' in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20223 min read

banner

Photo by Annie Spratt

"ModuleNotFoundError: No module named 'pip' in Python" #

The Python "ModuleNotFoundError: No module named 'pip'" occurs when pip is not installed in our Python environment. To solve the error, install the module by running the python -m ensurepip --upgrade command on Linux or MacOS or py -m ensurepip --upgrade on Windows.

no module named pip

Open your terminal and run the following command to install pip.

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

The ensurepip package enables us to bootstrap the pip installer into an existing Python installation or virtual environment.

If the PATH for pip is not set up on your machine, replace pip with python3 -m pip:

shell
# ๐Ÿ‘‡๏ธ make sure to use your version of Python, e.g. 3.10 python3 -m pip install requests

Alternatively, you can use the official get-pip script to install pip.

Download the script from https://bootstrap.pypa.io/get-pip.py by clicking on the link, 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

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

If none of the suggestions helped, try to install pip with a command that's specific to your operating system.

shell
# ๐Ÿ‘‡๏ธ On Debian / Ubuntu sudo apt update sudo apt install python3-venv python3-pip # ๐Ÿ‘‡๏ธ On MacOS brew install python # ๐Ÿ‘‡๏ธ On Fedora / CentOS sudo dnf install python3-pip python3-wheel

Try upgrading pip by running:

shell
# ๐Ÿ‘‡๏ธ on MacOS or Linux python -m pip install --upgrade pip # ๐Ÿ‘‡๏ธ for Python 3 python3 -m pip install --upgrade pip # ๐Ÿ‘‡๏ธ on Windows py -m pip install --upgrade pip

If the error persists, get your Python version and make sure you are installing the package using the correct Python version.

shell
python --version

get python version

For example, my Python version is 3.10.4, so I would install the requests package with pip3.10 install requests.

shell
pip3.10 install requests # ๐Ÿ‘‡๏ธ if you get permissions error sudo pip3.10 install requests

Notice that the version number corresponds to the version of Python I'm using.

If that didn't help and you're using a virtual environment, try recreating it.

shell
# ๐Ÿ‘‡๏ธ deactivate deactivate # ๐Ÿ‘‡๏ธ remove the old virtual environment folder rm -rf venv # ๐Ÿ‘‡๏ธ initialize a new virtual environment python3 -m venv venv # ๐Ÿ‘‡๏ธ activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ install the modules in your requirements.txt file pip install -r requirements.txt

Your virtual environment will use the version of Python that was used to create it.

This should work if your previous virtual environment didn't have pip installed for some reason.

If none of the suggestions helped on a Windows machine, try installing pip from the directory where the pip.exe file is located.

First locate Python by running the following command using cmd:

shell
# ๐Ÿ‘‡๏ธ for Windows where python # ๐Ÿ‘‡๏ธ or a specific version if you have multiple installed where python3

Now open the Scripts folder and make sure it contains the pip.exe file.

Open your command prompt in the Scripts directory right next to the pip.exe file and run the following command.

shell
pip install pip py -m pip install --upgrade pip
If the error persists, I would suggest watching a quick video on how to set Python and pip in your PATH environment variable.

This one is for setting Python and pip in the PATH on Windows:

This one is for setting Python and pip in the PATH on MacOS and Linux:

Use the search field on my Home Page to filter through my more than 1,000 articles.

Copyright ยฉ 2022 Borislav Hadzhiev