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
Problem Formulation
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:
import pip
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 :
python get-pip.py
β 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
- The
ModuleNotFoundError
may 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
pip
for Python 3, you may want to trypython3 -m pip ...
or evenpip3 install ...
instead ofpip 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
pip
here. - 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
import pip
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
import
statement. 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 ImportError
and ModuleNotFoundError
?
Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError
is a subclass of the ImportError
class.
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 ImportError
.
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 ModuleNotFoundError
.
Related Videos
The following video shows you how to resolve the ImportError
:
The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError
: