ModuleNotFoundError after pip install almost always means pip installed the package for one Python, and you ran your code with a different one. The fix is to install with the exact interpreter that runs your script:
python -m pip install tabulate # not just "pip install"
python your_script.py
Every screen below is a real Command Prompt on Windows with Python 3.12.5, using two separate environments so you can watch the mismatch happen.
You’ll learn how to check which Python is running, the fix for the command line, VS Code and Jupyter, and the other causes worth ruling out when the first fix doesn’t work.
Why ModuleNotFoundError happens after pip install
Most machines end up with more than one Python: a system install, a virtual environment or two, maybe Anaconda. Each one has its own set of installed packages.
pip belongs to one of them. If that isn’t the Python running your script, the package is installed, just somewhere your script can’t see. Here it is happening:
from tabulate import tabulate
sales = [["Austin", 1240.50], ["Dallas", 860.00], ["Houston", 1530.00]]
print(tabulate(sales, headers=["Store", "Sales (USD)"], floatfmt=".2f"))
Command Prompt output:
Requirement already satisfied: tabulate in c:\pyguides\venv\lib\site-packages (0.10.0)
Traceback (most recent call last):
File "C:\pyguides\report.py", line 1, in <module>
from tabulate import tabulate
ModuleNotFoundError: No module named 'tabulate'
C:\pyguides\venv. The script ran with app_env, which doesn’t have it.Both statements are true at once: pip really did install tabulate, and this Python really can’t import it. That’s why the error is so confusing the first time.
Which Python does pip install into, and which one runs your code?
Two lines tell you everything. sys.executable is the interpreter running the script, and importlib.util.find_spec() says whether that interpreter can see the package:
import importlib.util
import sys
print("running on:", sys.executable)
spec = importlib.util.find_spec("tabulate")
print("tabulate :", spec.origin if spec else "NOT installed for this Python")
Output:
running on: C:\pyguides\venv\Scripts\python.exe
tabulate : C:\pyguides\venv\Lib\site-packages\tabulate\__init__.py
running on: C:\pyguides\app_env\Scripts\python.exe
tabulate : NOT installed for this Python
pip 24.2 from C:\pyguides\venv\Lib\site-packages\pip (python 3.12)
pip 24.2 from C:\pyguides\app_env\Lib\site-packages\pip (python 3.12)
pip -V shows which Python each pip belongs to.Compare the path from sys.executable with the path in pip -V. If they point at different folders, you’ve found your problem.
While you’re checking, check your Python version too, since an old interpreter can also lack a package you installed for a newer one.
The fix: python -m pip install
Run pip as a module of the Python you actually use. python -m pip can’t pick the wrong interpreter, because it is that interpreter:
from tabulate import tabulate
sales = [["Austin", 1240.50], ["Dallas", 860.00], ["Houston", 1530.00]]
print(tabulate(sales, headers=["Store", "Sales (USD)"], floatfmt=".2f"))
Command Prompt output:
Collecting tabulate
Downloading tabulate-0.10.0-py3-none-any.whl.metadata (40 kB)
Downloading tabulate-0.10.0-py3-none-any.whl (39 kB)
Installing collected packages: tabulate
Successfully installed tabulate-0.10.0
Store Sales (USD)
------- -------------
Austin 1240.50
Dallas 860.00
Houston 1530.00
app_env itself, and the script runs.On Windows, py -m pip install tabulate does the same through the Python launcher. On macOS and Linux, use python3 -m pip install tabulate.
I’ve stopped typing a bare pip install altogether. The extra python -m costs nothing and removes this whole class of error.
ModuleNotFoundError in a virtual environment
A virtual environment is a separate Python with its own packages, which is exactly the setup in the demo above. Install into it while it’s active, then run from the same window:
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS / Linux
python -m pip install tabulate
python report.py
If you open a new terminal, it won’t be active until you activate it again. Virtual environments in Python walks through creating and managing them.
ModuleNotFoundError in VS Code
VS Code runs your file with whichever interpreter is selected for the workspace, and that’s often not the one your terminal’s pip installs into.
- Open the Command Palette with Ctrl+Shift+P (Cmd+Shift+P on a Mac).
- Run Python: Select Interpreter and pick the environment you installed the package into.
- Open a new terminal in VS Code, so it uses that same interpreter, and run
python -m pip installthere if needed.
The selected interpreter also shows in the status bar at the bottom right, so you can confirm it at a glance. PyCharm vs VS Code compares how the two editors handle this.
ModuleNotFoundError in Jupyter Notebook
A notebook runs on a kernel, and the kernel can be a different Python from the terminal you ran pip in. Install from inside the notebook so it lands in the kernel’s environment:
%pip install tabulate
Restart the kernel afterward if the import still fails. Avoid !pip install, which runs whichever pip the shell finds first and can repeat the original mistake.
The pip package name isn’t always the import name
If the interpreter is right and the error remains, check the name. Some packages install under one name and import under another:
| pip install | import |
|---|---|
beautifulsoup4 | bs4 |
scikit-learn | sklearn |
pillow | PIL |
python-dateutil | dateutil |
opencv-python | cv2 |
PyYAML | yaml |
Spelling and capitalization matter in the import line too. For library-specific cases, see No module named matplotlib, No module named django and No module named tkinter.
ModuleNotFoundError checklist
| Check | How |
|---|---|
| Which Python runs the script? | import sys; print(sys.executable) |
| Which Python does pip belong to? | pip -V |
| Can this Python see the package? | importlib.util.find_spec("name") |
| Install for the right Python | python -m pip install name |
| Virtual environment active? | Activate it in the terminal you run from |
| VS Code | Python: Select Interpreter |
| Jupyter | %pip install name, then restart the kernel |
| Import name differs? | pip show name and check the package docs |
If your import still fails, these guides cover the related problems:
- Virtual environments in Python
- Check your Python version
- Import a Python file from the same directory
- Fix No module named matplotlib
- Fix No module named django
Frequently asked questions
Why do I get ModuleNotFoundError after pip install?
Because pip installed the package for a different Python than the one running your code. Run python -m pip install package with the same interpreter you use to run the script.
What’s the difference between pip install and python -m pip install?
pip runs whichever pip is first on your PATH. python -m pip runs the pip that belongs to that exact python, so the package always goes where your script can see it.
How do I know which Python my script is using?
Print sys.executable from inside the script. Then compare it with the path shown by pip -V.
Why does it work in the terminal but not in VS Code?
VS Code uses the interpreter selected for the workspace. Run Python: Select Interpreter from the Command Palette and choose the environment where the package is installed.
How do I install a package in Jupyter Notebook?
Use %pip install package in a cell, which installs into the notebook kernel’s environment, then restart the kernel.
Where is installing packages documented?
The official Installing Python Modules guide recommends the -m switch so you run the right copy of pip when several Pythons are installed.
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn