Skip to content

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

Key points

  • Use python -m pip so that pip and Python match.
  • Check which interpreter is really running.
  • A file named like a library shadows it.
  • Running a script changes sys.path.

What it means

Python walks sys.path looking for the module name. If nothing matches, it raises ModuleNotFoundError. The package may be perfectly installed, just for a different Python.

Common causes

  • The virtual environment is not active.
  • pip belongs to another Python.
  • The install name differs from the import name: pip install Pillow, then import 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.

Diagnose it

Have Python report its own view.

bash
# 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.

Frequently asked questions

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.

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.