Python ModuleNotFoundError: No module named 'X'
No module named 'X' — import fails
Verified against CPython 3.13 source (Lib/importlib/), Python docs: tutorial/modules, Stack Overflow #1471994 · Updated April 2026
> quick_fix
The package isn't installed in the Python interpreter you're running. Install it with pip (inside your activated virtualenv), or confirm you're running the right Python.
# 1. Check which Python you're using
which python
which pip
# 2. Install the missing package
pip install <package_name>
# 3. If still failing, install for this specific Python
python -m pip install <package_name>What causes this error
CPython searches sys.path for modules in order: current directory, PYTHONPATH entries, and the site-packages of the active interpreter. If the module is not in any of those locations, or if you installed it into a different interpreter (e.g., system Python vs virtualenv), the import fails with ModuleNotFoundError.
How to fix it
- 01
step 1
Confirm which Python and pip you're using
Run `which python` and `which pip`. Both should point to the same interpreter (same path prefix). If not, you installed the package for a different Python.
which python which pip python --version pip --version - 02
step 2
Install using the interpreter's pip
The safest install command is `python -m pip install <package>` — this guarantees pip runs for the same Python you're using.
python -m pip install requests - 03
step 3
Check your virtualenv is activated
If you're in a venv, your shell prompt should show `(venv)`. If it doesn't, activate with `source venv/bin/activate` (mac/Linux) or `venv\Scripts\activate` (Windows).
- 04
step 4
For local imports, check PYTHONPATH or add __init__.py
For your own modules, Python treats a folder as a package only if it contains an __init__.py file (or is on sys.path). For relative imports inside a package, use `from .module import x`.
Frequently asked questions
Why does pip install work but the import still fails?
Because pip and python are pointing at different interpreters. Use `python -m pip install <package>` to force the same Python.
How do I install a package just for this project?
Create a virtualenv: `python -m venv .venv && source .venv/bin/activate && pip install <package>`. The virtualenv's Python has its own site-packages.
ModuleNotFoundError vs ImportError — what's the difference?
ModuleNotFoundError is a subclass of ImportError introduced in Python 3.6. It specifically means the module itself was not found. ImportError can also fire if the module exists but a specific name isn't in it.