Quick Fix: Python raises the
ImportError: No module named 'pip' when it cannot find the library
pip. The most frequent source of this error is that you haven’t installed
pip. Alternatively, you may have different Python versions on your computer, and
pip is not installed for the particular version you’re using.
I have compiled the following commands from various sources online — choose the one that applies to you – this should fix ~80% of the issues:
# 👇 For Python 3 (Linux with apt) sudo apt install python3-pip # 👇 For Python 2 (Linux with apt) sudo apt install python-pip
# 👇 For Python 3 (macOS, Win)
python3 -m ensurepip # 👇 For Python 3 (macOS, Win) python3 -m pip
# 👇 For Python 3 (macOS) brew install python # 👇 For Python 2 (macOS) python2.7 -m ensurepip --default-pip
# 👇 For Python 3 (macOS) python3 -m ensurepip --default-pip
# 👇 After installing ez_setup easy_install pip
You’ve just learned about the awesome capabilities of the
pip library and you want to try it out, so you start your code with the following statement:
This is supposed to import the Pandas library into your (virtual) environment. However, it only throws the following
ImportError: No module named pip:
>>> import pip Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import pip ModuleNotFoundError: No module named 'pip'
Please note that there are many other ways this error may occur (e.g., when attempting to install a library using pip in your shell or terminal). Try the commands suggested at the beginning. ✅
Solution Idea 1: Install Pip (Windows)
Download get-pip.py file in the desired folder, open the command prompt and navigate to the location of the downloaded file and install PIP using the following command :
✅ Recommended: How To Install pip On Windows?
If you want to install pip, also make sure to check out my in-depth guide:
✅ Recommended: How To Install pip? 5 Easy Steps
Other Solution Ideas
ModuleNotFoundErrormay appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
- You may have mixed up Python and pip versions on your machine. In this case, to install
pipfor Python 3, you may want to try
python3 -m pip ...or even
pip3 install ...instead of
pip install ...
- If you face this issue server-side, you may want to try the command
pip install --user ...
- If you’re using Ubuntu, you may want to try this command:
sudo apt install pip
- You can check out our in-depth guide on installing
- You can also check out this article to learn more about possible problems that may lead to an error when importing a library.
Understanding the “import” Statement
In Python, the
import statement serves two main purposes:
- Search the module by its name, load it, and initialize it.
- Define a name in the local namespace within the scope of the
importstatement. This local name is then used to reference the accessed module throughout the code.
What’s the Difference Between ImportError and ModuleNotFoundError?
What’s the difference between
Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the
ModuleNotFoundError is a subclass of the
You can see this in this screenshot from the docs:
You can also check this relationship using the
issubclass() built-in function:
>>> issubclass(ModuleNotFoundError, ImportError) True
Specifically, Python raises the
ModuleNotFoundError if the module (e.g.,
pip) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an
If an import statement cannot import a module, it raises an
ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a
The following video shows you how to resolve the
The following video shows you how to import a function from another folder—doing it the wrong way often results in the
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.