The package is installed and Python says it doesn't exist. Both are true: pip and python can belong to different installations. Here's how to see which one is which and how to make them agree.
Adapted from the Python Essentials Companion Guide.
You need a library, so you install it, and pip reports success:
$ pip install termcolor
Successfully installed termcolor-2.4.0
Then you run your script:
$ python app.py
Traceback (most recent call last):
File "app.py", line 1, in <module>
import termcolor
ModuleNotFoundError: No module named 'termcolor'
Both messages are telling the truth, which is exactly why this is so frustrating. pip installed termcolor. Python can't find termcolor. Nothing is broken, and nothing was lost. The package is sitting on your machine, just not where the Python that ran your script is looking.
What's actually happening
Most machines have more than one Python on them: the one your operating system ships with, one you installed yourself, one inside a virtual environment, one bundled with an editor or tool. Each of those has its own separate folder of installed packages, called site-packages. A package installed into one copy is invisible to every other copy.
The pip command and the python command are two separate programs that each get found by searching your PATH. Nothing guarantees they point at the same installation. So pip install termcolor may have put the package into Python A's site-packages, while python app.py started Python B, which has never heard of it. When import termcolor runs, Python B searches only its own folders, comes up empty, and raises ModuleNotFoundError.
That's the whole error: "I searched every place this interpreter looks, and the module isn't in any of them." It says nothing about whether the package exists elsewhere on your computer.
The fix, step by step
1. Find out which Python actually ran your script. Add this to the top of the file, or run it in the same terminal:
python -c "import sys; print(sys.executable)"
The path it prints is the interpreter that failed to find your package, for example C:\Python312\python.exe or /usr/bin/python3.
2. Find out where pip installs things.
pip --version
The output ends with a path and a Python version, something like pip 24.0 from /home/you/.venv/lib/python3.12/site-packages/pip (python 3.12). If that path points to a different Python than the one from step 1, you've found the mismatch.
3. Install through the interpreter itself, so the two can't disagree. Instead of running pip directly, run it as a module of the exact python you use:
python -m pip install termcolor
On Windows, if you run scripts with the launcher, use py -m pip install termcolor. Because python -m pip is pip belonging to that interpreter, the package lands in the same site-packages your script will search. Run your script again and the import works.
4. If you use a virtual environment, activate it before you install or run anything. The activated environment puts its own python and pip at the front of your PATH:
source .venv/bin/activate # macOS / Linux
.venv\Scripts\activate # Windows
You can confirm it worked: sys.executable from step 1 should now point inside your .venv folder.
Two mistakes worth knowing about ahead of time
Reinstalling the package over and over. Running pip install again feels like it should help, and it only reports "Requirement already satisfied", because pip is checking the installation it belongs to, which already has the package. The repeated message is a sign the install is fine and the interpreter is the problem.
Using the wrong name for the import. Sometimes the package you install and the module you import have different names, and then you get this same error with a perfectly matching interpreter. pip install python-dotenv gives you import dotenv, not import python_dotenv. Other common pairs are Pillow (import PIL), scikit-learn (import sklearn), and opencv-python (import cv2). If python -m pip show <package> finds it but the import still fails, check the package's documentation for the real import name.
Editors add one more layer to this. If the script runs fine in your terminal but your editor shows the same error, the editor is using a different interpreter than your terminal. That case has its own explanation in VS Code Is Running the Wrong Python.*).
A habit that prevents the confusion entirely
Get in the habit of typing python -m pip install ... instead of bare pip install ..., every time. It costs a few extra keystrokes and removes the entire class of problem, because the pip you run is by construction the one that belongs to the Python you named. Pair it with one virtual environment per project, activated before you start working, and "it's installed but Python can't see it" becomes something you can diagnose in under a minute: sys.executable tells you who ran, pip --version tells you who installed, and the two paths either match or they don't.
If you'd like more posts like this sent straight to your inbox, subscribe to the newsletter.
Prefer to dig in yourself? The Python Essentials repo on GitHub has more free examples and exercises.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.