You install a package with pip, run your Python script, and it immediately fails saying the module does not exist. This is one of the most frustrating Python errors because it often happens right after you supposedly installed the thing it cannot find. The good news is there are only a handful of causes, and the most common one is that pip installed the package for a different Python than the one running your code.
Cause 1: pip and python point to different installs
This is the number one cause. Many computers have more than one Python installed, for example a system Python and one you downloaded, or both Python 2 and Python 3. When you type pip install pandas, pip might install into one Python, while python script.py runs a different one. The package is installed, just not where your code is looking.
The reliable fix: install with python -m pip
The cleanest solution is to install the package using the exact same Python that runs your script. Instead of calling pip on its own, run pip through python:
# Installs into the same Python that runs your code
python -m pip install pandas
On systems where you run your scripts with python3, match that:
python3 -m pip install pandas
This guarantees the install and the run use the same interpreter, which fixes the most common version of this error outright. To confirm which Python you are actually running, check:
# Shows the exact interpreter path
python -c "import sys; print(sys.executable)"
Cause 2: a virtual environment is not active
If your project uses a virtual environment, packages installed inside it are invisible unless the environment is activated. You may have installed the module into the venv, then opened a new terminal where the venv is not active, so the module appears missing.
Activate the environment before running, then install or run:
# Windows
venv\Scripts\activate
# macOS and Linux
source venv/bin/activate
# your prompt now shows (venv), then install or run normally
python -m pip install pandas
When a virtual environment is active, your terminal prompt usually shows its name in parentheses, like (venv). If you do not see that, the environment is not active, which alone explains many of these errors.
Cause 3: import name vs package name
Sometimes the name you install is not the name you import. The package on pip and the module in your code can differ, and importing the wrong one gives a ModuleNotFoundError even though the package is installed. A few common mismatches:
| You install | You import |
|---|---|
pip install pillow | import PIL |
pip install beautifulsoup4 | import bs4 |
pip install opencv-python | import cv2 |
pip install scikit-learn | import sklearn |
If you are sure the package installed but the import fails, check the project’s documentation for the correct import name. It is often different from the install name.
Cause 4: your file shadows the module
If you name your own file the same as a module you are importing, Python finds your file instead of the real library, and the import breaks. For example, if you save a script as random.py and then write import random, Python imports your empty file, not the real random module:
# File saved as: math.py
import math
math.sqrt(16) # fails, because math.py is your own file
The fix is to rename your file to something that is not a module name, such as my_math.py, and delete any math.pyc or __pycache__ left behind. Then the real module imports correctly.
Cause 5: your editor uses a different interpreter
If the code runs fine in the terminal but fails inside your editor, the editor is configured to use a different Python interpreter than the one where you installed the package. In VS Code, open the command palette and choose Python: Select Interpreter, then pick the same interpreter or virtual environment where the module is installed. After selecting the right one, the import resolves.
Frequently asked questions
I ran pip install and it said “already satisfied” but I still get the error. Why?
That message means the package is installed for the pip you called, but your script is running a different Python. Use python -m pip install with the same python command you use to run your code. The “already satisfied” was true for the wrong interpreter.
What is the difference between ModuleNotFoundError and ImportError?
ModuleNotFoundError is a specific type of ImportError that means the module could not be found at all. A plain ImportError can also mean the module was found but something inside it failed to import. For “No module named” messages, you are dealing with the module simply not being on the path your interpreter searches.
How do I see which packages are installed for my Python?
Run python -m pip list using the same python command you run your scripts with. It prints every installed package for that exact interpreter, so you can confirm whether the module you need is actually there.
Should I always use a virtual environment?
For any real project, yes. A virtual environment keeps each project’s packages separate and avoids most version and path conflicts that cause this error. Create one per project, activate it, and install everything inside it.
Most of the time, installing with python -m pip and confirming your interpreter solves this immediately. Once your environment is sorted, any project from the Python source code library will run with its imports intact.

