ModuleNotFoundError: No module named 'modulename'
The fastest fix is to install the missing package. Open a terminal in your project directory and run:
pip install modulename
# or if you have multiple Python versions:
python3 -m pip install modulename
If that does not work, read on — there are five other common causes.
Last updated: March 2026
The Full Error
Traceback (most recent call last):
File "app.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
ModuleNotFoundError is a subclass of ImportError introduced in Python 3.6. In Python 2 and early Python 3, the same situation raised a plain ImportError.
Cause 1: Package Is Not Installed
The package simply does not exist in the Python environment you are running.
Fix:
pip install requests
Verify it installed:
pip show requests
# Name: requests
# Version: 2.31.0
# ...
Cause 2: Wrong Python Version — pip vs pip3 vs python -m pip
Your system may have Python 2 and Python 3 installed. pip might install into the Python 2 site-packages while your script runs under Python 3 (or vice versa).
Diagnosis:
python --version # e.g. Python 2.7.18
python3 --version # e.g. Python 3.11.4
pip --version # pip 23.2 from .../python3.11/... (python 3.11)
Fix — always use python -m pip to install into the same Python that runs your script:
python3 -m pip install requests
# then run with the same python3
python3 app.py
This guarantees that pip and python refer to exactly the same installation.
Cause 3: Virtual Environment Is Not Activated
If you created a venv but forgot to activate it, you install packages into the global Python while your environment is separate.
Check if a venv is active:
echo $VIRTUAL_ENV # empty = no venv active
# or
which python # should point inside your project directory, e.g. .venv/bin/python
Activate the venv:
# Linux / macOS
source .venv/bin/activate
# Windows (cmd)
.venv\Scripts\activate.bat
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
After activation, install the package:
pip install requests
Cause 4: Wrong Working Directory or sys.path Problem
Python searches for modules in the directories listed in sys.path. If your module is in a custom location that is not on that list, Python will not find it.
Diagnose:
python3 -c "import sys; print('\n'.join(sys.path))"
You should see the current directory (empty string '' or .) and the site-packages folder for your environment. If a custom module directory is missing, add it:
import sys
sys.path.insert(0, "/path/to/your/module/directory")
import mymodule
A better long-term solution is to install the package properly with pip install -e . if it is your own code, or to set PYTHONPATH:
export PYTHONPATH=/path/to/your/module/directory:$PYTHONPATH
python3 app.py
Cause 5: Package Name Differs From Import Name
The name you use in pip install is sometimes different from the name you use in import. This trips up even experienced developers.
pip install ... |
import ... |
|---|---|
Pillow |
PIL |
scikit-learn |
sklearn |
beautifulsoup4 |
bs4 |
python-dateutil |
dateutil |
opencv-python |
cv2 |
pyyaml |
yaml |
mysql-connector-python |
mysql.connector |
Fix: Check the package's PyPI page or README for the correct import name.
Cause 6: Namespace Conflict — Your Own File Shadows the Module
If you have a file named requests.py (or json.py, os.py, etc.) in the same directory as your script, Python will import your file instead of the real package.
Fix: Rename your file to something that does not conflict with standard library or third-party module names.
Debugging sys.path Step by Step
# 1. Find which Python you are actually using
which python3
# 2. Check its sys.path
python3 -c "import sys; print(sys.path)"
# 3. Check if the package is installed
python3 -c "import requests; print(requests.__file__)"
# 4. List all installed packages
pip list | grep requests
Common Modules and Their Install Commands
| Import name | pip install command |
|---|---|
requests |
pip install requests |
numpy |
pip install numpy |
pandas |
pip install pandas |
PIL |
pip install Pillow |
sklearn |
pip install scikit-learn |
bs4 |
pip install beautifulsoup4 |
cv2 |
pip install opencv-python |
yaml |
pip install pyyaml |
dotenv |
pip install python-dotenv |
dateutil |
pip install python-dateutil |
FAQ
Q: I ran pip install X and it says "Successfully installed" but the error still occurs.
A: You have multiple Python installations. Run python3 -m pip install X instead, then run your script with python3 app.py. This guarantees pip and python use the same environment.
Q: The error only happens inside my Docker container or CI pipeline, not locally.
A: Your requirements file is missing the package. Add it: pip freeze > requirements.txt locally, or explicitly add the package to requirements.txt, then rebuild the container.
Q: I see ModuleNotFoundError: No module named 'mypackage' for my own code.
A: Python does not know about your package unless it is on sys.path or installed. For a package you are developing, run pip install -e . from the directory containing your setup.py / pyproject.toml to install it in editable mode.
Related Pages
- Most Common Python Errors and How to Fix Them — overview of all common Python exceptions
- Fix Python AttributeError — 'X' object has no attribute 'Y'