Fix: ModuleNotFoundError: No module named 'X'
Exact message
ModuleNotFoundError: No module named 'requests'
Quick answer
ModuleNotFoundError means Python cannot find the package you tried to import. The three common causes are: the package is not installed, you are in a different virtual environment, or the module name is misspelled.
Why this error happens
When Python executes import X, it searches sys.path for a matching package. If none exists — because pip install was never run, or the active virtualenv is different from the one you installed into — Python raises ModuleNotFoundError. On rare occasions the package exists but is named differently from the import statement.
Example: Wrong vs Right
Wrong
import requests # ModuleNotFoundError: No module named 'requests'
Right
# Step 1: install the package # pip install requests # Step 2: verify it is available import requests print(requests.__version__)
How to fix it (step by step)
- Run pip list | grep <package> to confirm it is installed in the current environment
- If missing, run pip install <package> — the name you pip install may differ from the import name (e.g. pip install pillow, import PIL)
- Check which Python you are using: which python and which pip must point to the same virtualenv
- Restart your terminal or IDE so the PATH changes take effect
Frequently asked questions
Why does pip install succeed but import still fails?
You likely installed into a different Python interpreter. Run python -m pip install <package> so pip uses the same Python that runs your script.
Does this happen in Docker?
Yes. If your Dockerfile installs the package in one layer and runs your script in another Python image, the import fails. Install and run in the same image.
What about relative imports?
The error message differs — 'attempted relative import with no known parent package' — usually fixed by running as a module: python -m package.module.
Related Python errors
Need a fix for a different error?
Use the free to paste any traceback and get an AI-suggested fix in seconds.
Want an AI with memory for your project?
MonsterX AI remembers your codebase across chats.