πŸ”§ Error Fixes

Python ModuleNotFoundError β€” How to Fix 'No module named'


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)"