Troubleshooting guide
ModuleNotFoundError in Python: how to fix it
Python could not find the module in any directory on `sys.path`. Usually, the package was installed for a different interpreter.
3 min read10 sectionsUpdated
On this page
Common causes
- The virtual environment is not active.
pipbelongs to another Python.- The install name differs from the import name:
pip install Pillow, thenimport PIL. - Relative imports when running a file inside a package directly.
- A dependency missing from
requirements.txt, so it works locally only. - Multi-stage Docker builds that do not copy site-packages.
# Which interpreter and paths?
python -c "import sys; print(sys.executable); print(*sys.path, sep='\n')"
# Is pip tied to the same interpreter?
python -m pip --version
# Is it installed, and where?
python -m pip show requests
# Inside the image
docker run --rm myimage python -c "import requests; print(requests.__file__)"Fixes
Install with python -m pip install package. Activate the venv, or call its binary directly: .venv/bin/python app.py. Run packages as modules: python -m mypkg.main from the project root. Pin all dependencies in a lock file, and build images from it. In a multi-stage Dockerfile, copy the virtual environment across, or install with a known target path.
The shadowing trap
If your project contains random.py, email.py or requests.py, then import requests finds your file first. The resulting error often reads like a missing attribute or a circular import rather than a missing module, which makes it confusing. Rename the file and delete any stale __pycache__ directory. Similarly, a missing __init__.py is rarely fatal since namespace packages arrived, but tools such as test runners may still resolve paths differently, which explains why code works from one directory and fails from another.
Catch it automatically
A missing dependency fails on import, so a smoke test in CI that imports your entry point catches this before release. After deployment, a crash-looping container and an uptime check show it right away.
Why does pip say it is installed?
It was installed for a different Python. Use python -m pip.
Why does it work in my IDE but not in the terminal?
They use different interpreters.
What is the difference from ImportError?
ModuleNotFoundError is the subclass raised specifically when a module cannot be located.
Related reading
- Cloudflare error 1016 "Origin DNS error": how to fix it: Fix Cloudflare error 1016 Origin DNS error: CNAME records pointing to names that do not resolve, deleted origins and load balancer pools, with checks.
- 400 Bad Request: what it means and how to fix it: Fix a 400 Bad Request error: oversized cookies, malformed URLs, invalid JSON bodies and proxy rules, with commands to find which layer rejected it.
- 422 Unprocessable Entity: how to fix it: Fix 422 Unprocessable Entity (Unprocessable Content): validation errors in APIs and forms, CSRF tokens in Rails and Laravel, and how to read the error body.
- 501 Not Implemented: how to fix it: Fix 501 Not Implemented: unsupported HTTP methods, servers and proxies that do not support PATCH or WebDAV, and how it differs from 405 and 500.
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- Cloudflare error 521: web server is down: Fix Cloudflare error 521 "Web server is down": the origin refused the connection. Check the web server, firewall rules for Cloudflare IPs and DNS.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.