ModuleNotFoundError: No module named 'requests'
Python canβt find the module youβre trying to import. This is almost always one of these six causes.
Fix 1: Install the Package
The most common cause β the package isnβt installed.
pip install requests
# If you have multiple Python versions
pip3 install requests
python3 -m pip install requests
Check if itβs installed:
pip list | grep requests
pip show requests
Fix 2: Activate Your Virtual Environment
You installed the package globally but your project uses a virtual environment (or vice versa).
# Check if you're in a venv
which python
# If it shows /usr/bin/python instead of ./venv/bin/python, you're not in a venv
# Activate it
source venv/bin/activate # Linux/Mac
.\venv\Scripts\activate # Windows
# Then install
pip install requests
Common scenario: You installed with pip install in your terminal, but VS Code is using a different Python interpreter. Check the bottom-left of VS Code β make sure it points to your venvβs Python.
Fix 3: Wrong Python Version
You installed for Python 3.9 but youβre running Python 3.12.
# Check which Python you're using
python --version
which python
# Install for the specific version
python3.12 -m pip install requests
# Or use the same Python that runs your script
python3 -m pip install requests
python3 script.py
Fix 4: Package Name β Import Name
Some packages have different install names and import names.
# Install name β Import name
pip install Pillow # import PIL
pip install python-dotenv # import dotenv
pip install scikit-learn # import sklearn
pip install opencv-python # import cv2
pip install beautifulsoup4 # import bs4
pip install PyYAML # import yaml
Fix 5: Circular Import
Two files importing each other causes this error.
# β file_a.py
from file_b import something
# β file_b.py
from file_a import something_else # π₯ circular!
Fix: Restructure your code so the shared logic is in a third file, or move the import inside the function that needs it:
# β
Import inside function (lazy import)
def my_function():
from file_b import something
return something()
Fix 6: Path Issues
Python canβt find your local module because itβs not in the Python path.
# Check Python's search path
python -c "import sys; print('\n'.join(sys.path))"
Fix for local modules:
# Add parent directory to path
import sys
sys.path.insert(0, '..')
# Or set PYTHONPATH
export PYTHONPATH="${PYTHONPATH}:/path/to/your/project"
Fix for project structure: Make sure you have __init__.py files in your package directories:
my_project/
βββ __init__.py
βββ main.py
βββ utils/
βββ __init__.py β this file is required
βββ helpers.py
Debugging Checklist
# 1. Which Python am I using?
which python && python --version
# 2. Am I in a virtual environment?
echo $VIRTUAL_ENV
# 3. Is the package installed?
pip show <package-name>
# 4. Where is Python looking for modules?
python -c "import sys; print(sys.path)"