Explain Codes LogoExplain Codes Logo

Error running 'pip install': "ImportError: No module named pip"

python
pip-installation
python-versions
virtual-environments
Anton ShumikhinbyAnton Shumikhin·Feb 6, 2025
TLDR

The crux of ImportError: No module named pip is that pip is absent in your system. Fix it with:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python get-pip.py

For the Python 3 users out there, replace the last part with python3. This nifty command fetches the get-pip.py script and runs it, giving your system the super power of pip for package management.

Check your pip installation

Post running the command, you will see a 'Collecting pip' message, your ticket to the successful installation confirmation party. Inspect further with pip --version or, for Python 3 users, pip3 --version.

Keep Python updated or install from scratch

Ensure your version of Python is the latest and greatest:

  • On Linux: sudo apt-get update && sudo apt-get upgrade python3
  • On macOS with Homebrew: brew update && brew upgrade python

If you are craving a fresh Python install on macOS with pip included, cook it up with this recipe:

brew install python

Virtual environment etiquette

Always say "activate your virtual environment, please" before wielding any pip installation commands:

source /path/to/venv/bin/activate

Remember, pip installs within the active environment like a polite house guest, ensuring the sanctity of your project's isolation.

Python and pip version mix-and-match

You'll want to match the right version of pip with your Python. For Python's 2.x fans club, use:

sudo apt install python-pip

For those aboard Python 3.x spaceship, signal this command:

sudo apt install python3-pip

If you are living life on the edge with Python 2.7, don't shy away from:

python2.7 -m ensurepip --default-pip

Didn't pip install work? Let's troubleshoot

If pip installation is throwing tantrums, fear not! The user-friendly easy_install is ready for your call:

easy_install pip

It's good housekeeping to uninstall any old versions of pip before installing a new one, because nobody likes a cluttered toolbox:

pip uninstall pip

Common pitfalls and how to avoid them

Monsters under the ImportError bed include:

  • Python version: Are you using the right version for your requirements?
  • Environment: Is it the right environment, which might already have pip installed?

Speaking in tongues: the right pip command

When dealing with polyglot systems where multiple Python versions speak different dialects, be very specific:

/usr/bin/python3.8 -m pip install package-name

This ensures you're speaking to pip from Python 3.8, and not linguistically confusing your system.