How to Fix Python "ModuleNotFoundError: No module named"
ModuleNotFoundError means Python searched every path and never found the module — usually the wrong interpreter or venv, or an install-name that differs from the import-name. Here is the fix.
ModuleNotFoundError: No module named 'x' means Python searched every entry on sys.path and never found a module or package with that name — so this is fundamentally about the interpreter or environment not having the package where *this specific Python* is looking. (ImportError: cannot import name … is different: the module was found, but a name inside it could not be pulled out.)
The 6 common causes and their fixes
1. The package is not installed for the active interpreter
Install into the exact Python running the code with python -m pip.
python -m pip install requests
# `python -m pip` guarantees pip targets the same interpreter as `python`
2. The wrong virtual environment or interpreter
The package is installed, but into a different venv or the system Python — very common in IDEs and Jupyter.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install requests
python -c "import sys; print(sys.executable)" # confirm which python
3. The install name is not the import name
The PyPI distribution name often differs from the importable module name.
pip install beautifulsoup4 -> import bs4
pip install Pillow -> from PIL import Image
pip install PyYAML -> import yaml
pip install scikit-learn -> import sklearn
pip install opencv-python -> import cv2
pip install python-dateutil -> import dateutil
4. Wrong directory, sys.path, or a shadowing file
A local file that shares a module’s name (e.g. your own random.py or tokenize.py) shadows the real one. Rename it, remove stray .pyc, and run from the project root as a module.
# a file named tokenize.py in the cwd breaks `import tokenize`
# ✅ rename the local file, then run as a module
python -m mypackage.mymodule
5. python2 vs python3 / pip vs pip3
pip may point at a different install than python3.
python3 -m pip install requests
python3 app.py
6. Missing package structure or a bad relative import
Running a file directly that uses a relative import raises attempted relative import with no known parent package.
# mypkg/mod.py contains: from . import helper
# ❌ python mypkg/mod.py
# ✅ run as a module so the parent package is known
python -m mypkg.mod
Quick diagnostic checklist
python -c "import sys; print(sys.executable)"— confirm which interpreter is actually running.python -m pip show <pkg>— is it installed for THAT interpreter? If not,python -m pip install <pkg>.- Check install-name vs import-name (beautifulsoup4→bs4, Pillow→PIL, opencv-python→cv2).
- Ensure no local file shadows the module, and launch from the project root (
python -m pkg.mod). - If it is
cannot import name …, that is an ImportError — look for a typo or a circular import, not a missing install.
Frequently asked questions
Why does pip install succeed but import still fails?
python -m pip install <pkg> and confirm with python -c "import sys; print(sys.executable)" and python -m pip show <pkg> that both point to the same Python.I installed beautifulsoup4 but "import beautifulsoup4" fails — why?
beautifulsoup4, import bs4. Other common ones: Pillow→PIL, PyYAML→yaml, scikit-learn→sklearn, opencv-python→cv2, python-dateutil→dateutil.What is the difference between ModuleNotFoundError and ImportError?
Run Python instantly — no local install, no venv confusion — in the XCODX online compiler. It executes Python in the browser so you can confirm a fix without touching your machine’s interpreters.