Tutorial5 min read

ModuleNotFoundError in Python: How to Fix It Every Time

ModuleNotFoundError is one of Python's most common errors. Here's why it happens, the 6 most common causes, and how to fix each one permanently.

PythonModuleNotFoundErrorImportErrorvirtual environmentpipdebugging

The Error

ModuleNotFoundError: No module named 'requests'

Python can't find the module you're importing. The fix is almost always one of 6 things.


Cause 1: Package Not Installed

pip install requests
pip show requests

Cause 2: Wrong Virtual Environment

which python     # macOS/Linux
where python     # Windows
pip show requests | grep Location

source venv/bin/activate    # macOS/Linux
venv\Scripts\activate       # Windows

This is the #1 cause of "but I already installed it" confusion.


Cause 3: Python Version Mismatch

python --version
python3.11 -m pip install requests

Always use python -m pip install (not just pip install) to guarantee you install into the Python that will run your code.


Cause 4: Local Module Not Found (Relative Import)

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

Or install in editable mode:

pip install -e .

Cause 5: Circular Import

Module A imports from Module B, Module B imports from Module A. Fix: move shared code to a third module that neither imports from the others.


##Some packages have different install names vs import names:

  • Pillow → import as PIL
  • scikit-learn → import as sklearn
  • python-dateutil → import as dateutil
  • opencv-python → import as cv2

The Fastest Debug Path

1. pip show <package> — is it installed at all?

2. which python / which pip — same environment?

3. Check for __init__.py if it's a local module

4. Check if install name ≠ import name

With DebugAI, press Ctrl+Shift+P — it reads your import structure and virtual environment context automatically.

Install DebugAI

Debug faster starting today.

Free VS Code extension. 10 sessions/day. No credit card.

Install Free →

Related Posts

Tutorial

Fix KeyError in Python: 5 Causes and How to Find the Source

5 min read

Tutorial

Fix IndentationError in Python: 6 Causes and Exact Fixes (2026)

5 min read

← All posts