ModuleNotFoundError: No module named 'x'

Python · updated 2026-10-09

Raised at an import statement, usually at the bottom of a short traceback that starts in your entry file. Common right after a deploy, a Python upgrade, or when the same script runs from cron, an IDE or a Docker image instead of your shell.

Traceback (most recent call last):
  File "/srv/app/worker.py", line 4, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

Quick fix

Install the package into the interpreter that runs the script: python -m pip install <package>, using the exact python command you run the code with. Check that interpreter with python -c "import sys; print(sys.executable)". If the install name differs from the import name (Pillow vs PIL), install the distribution name.

  1. Print sys.executable from the failing environment to see which Python runs.
  2. Install with that interpreter: /path/to/python -m pip install <package>.
  3. Use the project venv's python path in cron jobs, service files and IDE run configs.
  4. Check PyPI for the distribution name when it differs from the import name.
  5. Add the package to requirements.txt or pyproject.toml so deploys install it.
bash
python -c "import sys; print(sys.executable)"   # which interpreter runs your code
python -m pip install requests                  # install into that same interpreter
python -m pip show requests                     # confirm where it landed
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/modulenotfounderror.md and fix this error in this project.

Why it happens

pip belongs to a different Python

The pip on your PATH installs into one interpreter (system Python, Homebrew, pyenv, a venv) while python runs another. The install reports success, but into a site-packages directory your script never looks at. python -m pip removes the ambiguity.

Virtualenv not used where the code runs

cron, systemd units, Dockerfile CMD lines and IDE run configurations ignore the venv you activated in your shell and fall back to the system interpreter. Point them at .venv/bin/python explicitly.

Import name differs from the package name

Pillow is imported as PIL, beautifulsoup4 as bs4, scikit-learn as sklearn, PyYAML as yaml and opencv-python as cv2. Running pip install with the import name either fails or installs the wrong thing.

Running a module file as a script

python app/jobs/sync.py puts app/jobs on sys.path instead of the project root, so import app.models fails. Run python -m app.jobs.sync from the project root so absolute imports resolve.

Module removed from the standard library

Python 3.12 removed distutils and imp, and 3.13 removed cgi, telnetlib, pipes and other old modules. Dependencies that still import them break after an upgrade; upgrade the dependency, or install setuptools to get a distutils shim.

Docker image installs into the wrong place

Multi-stage builds that install into one stage and copy a different directory into the final image, or pip install run before switching to a non-root user with a different site-packages, leave the runtime image without the package.

Other fixes

  • On systems that block global installs (PEP 668, externally-managed-environment), create a venv with python -m venv .venv and install there.
  • In uv or Poetry projects, run the script through the tool (uv run, poetry run) so it uses the project environment.
  • Rename local files that shadow a package (email.py, test.py), which cause errors like No module named 'email.mime'; 'email' is not a package.

Same error, other wording

  • ImportError: No module named x
  • ModuleNotFoundError: No module named 'distutils'
  • ModuleNotFoundError: No module named 'x.y'; 'x' is not a package

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the ModuleNotFoundError events in production, tell me which module is missing and on which host or container image, and fix the dependency file or Dockerfile so that environment installs it.

Setup for Python and FastAPI, Django or Flask, then have your agent fix and resolve it.

Related errors

More in Imports and modules

Other error topics

Free tools: Source map stack trace decoder · Uptime and SLA downtime calculator · Error budget calculator · Stack trace formatter · JSON log viewer · HTTP status code reference.

References: Python: Built-in exceptions (ModuleNotFoundError), Python: Installing Python modules, Python: venv

Spotted a mistake? Email hello@greatwork.company. About Runheed.