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.
The Error
Python can't find the module you're importing. The fix is almost always one of 6 things.
Cause 1: Package Not Installed
Cause 2: Wrong Virtual Environment
This is the #1 cause of "but I already installed it" confusion.
Cause 3: Python Version Mismatch
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)
Or install in editable mode:
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.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.