Fix ModuleNotFoundError / ImportError in 5 Minutes — Cause-by-Cause Diagnostic Table and Copy-Paste Commands
Python Development Guide, Part 2 · If you set up the environment in Part 1 but got a wall of red text when you ran
python app.py, this post is for you.
"I definitely ran pip install — why am I still getting No module named?"
In Part 1 you created a venv and installed packages. Then you ran the app and got this:
ModuleNotFoundError: No module named 'requests'When a package is installed but still not found, the reason is almost always that the Python you installed into is not the Python you are running. The good news: import errors look infinite, but they collapse into exactly 7 causes.
- Never actually installed
- Wrong interpreter / venv not activated
sys.path·PYTHONPATHissues- Relative import errors
- Package name ≠ import name
- Circular import
- Missing
__init__.py
The goal of this post is to kill the guessing. Narrow the cause with diagnostic commands → look it up in the table → fix it with copy-paste.
30-second diagnostic routine: 4 commands that narrow the cause
When you are stuck, run these from top to bottom as-is. In 30 seconds you will see which Python is looking at which packages.
# 1) Which executable is 'python' actually pointing to
which python # Windows: where python
# 2) Which Python is 'pip' attached to
pip -V
# 3) The running Python and the full module search path
python -c "import sys; print(sys.executable); print(sys.path)"
# 4) Packages actually installed for that Python
python -m pip listHow you read each line is the key.
| Command | What you check | How to interpret it |
|---|---|---|
which python | Path of the Python that will run | A venv path (.../venv/bin/python) is healthy; /usr/bin/python suggests the venv is not active |
pip -V | The Python pip is bound to | The path at the end must match which python |
sys.executable | The interpreter that actually ran | If this does not point at the venv, you have a 100% interpreter mismatch |
python -m pip list | That Python's install list | If the package is missing here, it was never installed into this Python |
Remember one core rule: use python -m pip install, not pip install. That installs into "this Python, right now." PEP has long settled on python -m as the recommended pattern.
Diagnostic table by cause and copy-paste fixes
With those diagnostic results in hand, find your symptom in the table below.
| Error message | Cause | Diagnostic command | Fix command |
|---|---|---|---|
No module named 'X', not in the list either | ① Never actually installed | python -m pip list | grep X | python -m pip install X |
Installed but not found, which python is outside the venv | ② Interpreter / venv mismatch | Compare which python & pip -V paths | source venv/bin/activate then reinstall (Win: venv\Scripts\activate) |
| Only your local module is missing | ③ sys.path / PYTHONPATH | python -c "import sys;print(sys.path)" | export PYTHONPATH=$(pwd) or pip install -e . |
attempted relative import... | ④ Relative import error | Check how you run it | Run with python -m package.module |
No module named 'cv2' but you installed opencv | ⑤ Package name ≠ import name | Confirm the real name with python -m pip list | See the cheat sheet below (import cv2) |
ImportError: cannot import name 'A' | ⑥ Circular import | Trace import locations | Move the import inside a function / split the module |
Folder exists but No module named 'mypkg' | ⑦ Missing __init__.py | ls mypkg/ | touch mypkg/__init__.py |
Practical tip: Most import errors that blow up in real work converge on cause ② (interpreter mismatch) alone. If it works in the terminal but breaks only in the IDE, that is almost certainly it. So when you get stuck, print
sys.executablebefore you start theorizing.
Three traps that blow up constantly
① IDE interpreter mismatch (VS Code / PyCharm)
If it works in the terminal but the IDE Run button breaks it, the IDE is using a different Python than the venv.
VS Code
Ctrl/Cmd + Shift + P→Python: Select Interpreter- Pick the project venv path (
./venv/bin/python) from the list - To pin it, set it directly in
.vscode/settings.json:
{
"python.defaultInterpreterPath": "${workspaceFolder}/venv/bin/python"
}(Screenshot caption: Check that the interpreter path in the VS Code status bar at the bottom right points at the venv)
PyCharm
Settings → Project → Python Interpreter- Gear icon →
Add Interpreter → Add Local Interpreter - Choose
Existingand point it atvenv/bin/python
(Screenshot caption: The project venv selected in the Python Interpreter dropdown)
② Package name ≠ import name cheat sheet
Some packages have a different name for pip install than for import. If you do not know this, you will swear you installed it and still get No module named.
Install command (pip install) | Import (import) |
|---|---|
pillow | PIL |
opencv-python | cv2 |
beautifulsoup4 | bs4 |
scikit-learn | sklearn |
pyyaml | yaml |
python-dotenv | dotenv |
Flask-SQLAlchemy | flask_sqlalchemy |
Rule of thumb: hyphens become underscores on import, or the name changes entirely. When in doubt, check the real package info with python -m pip show pillow.
③ python script.py vs python -m package.module
99% of relative import (from . import utils) errors come from how you run the file.
# ❌ Running a file that uses relative imports directly will break
python myapp/main.py
# → attempted relative import with no known parent package
# ✅ Running it as a package gives it a parent-package context so it works
python -m myapp.mainWith -m, Python treats the module as "part of a package," which is what relative imports need.
2026 trend: Astral's
uvis quickly becoming the standard, and more teams are usinguv pip install/uv run.uv run python ...auto-picks the project venv, so the cause ② mismatch problem almost disappears. Even on Python 3.13, a per-project venv plus running withpython -mis still the safest default.
Wrap-up: finish it with one diagnostic table
When you get stuck, the order is always the same.
- Check that
which pythonandpip -Vpaths match → if not, activate the venv - Check that the package is in
python -m pip list→ if not,python -m pip install - If it is there and still fails → check the import-name cheat sheet and relative imports (
-mexecution)
Follow just these 3 steps from the top and import errors resolve in 5 minutes.
References: official docs
The primary sources for the behavior, settings, and errors covered here are the official docs below. Check them for version-specific options and exact behavior.
Frequently asked questions (FAQ)
Q. It works in the terminal but I get No module named only in Jupyter (.ipynb).
A. The notebook kernel is using a different Python. In a cell, run import sys; print(sys.executable) to see the kernel path. Even if you run !pip install inside the notebook, it can install into the wrong Python, not the kernel — use %pip install X (magic command) instead, or register the venv as a kernel:
python -m ipykernel install --user --name=myvenvThen select the myvenv kernel in Jupyter.
Q. The module is missing only inside a Docker container.
A. Check three things. ① Is the order COPY requirements.txt → RUN pip install correct (copy requirements before the full source so the build cache actually helps), ② Did you change dependencies but RUN pip install never re-ran because of cache (docker build --no-cache to verify), ③ Did the base image Python version differ from local so an incompatible wheel got installed. The fastest check is to run python -m pip list inside the container and see what is actually installed.
Q. pip install succeeded but at runtime it says the module is missing.
A. Classic case: you installed into the user site (pip install --user) but you are running system Python. Compare pip -V and which python paths, and always install with python -m pip install so it goes into "this Python, right now."
In the next post (Part 3) we cover Python debugging and exception handling in practice — how to read a traceback fast, plus using pdb / breakpoint().
Nodelog는 모든 콘텐츠의 내용과 출처를 공개 전에 검토합니다. 환경(OS·버전)에 따라 결과가 달라질 수 있는 기술 정보는 공식 문서와 함께 확인하며, 검토 기준과 정정 원칙은 편집 정책에서 안내합니다. 오류를 발견하시면 이메일로 제보해 주세요 — 확인 후 신속히 정정합니다.
Comments
Be the first to comment.