# ModuleNotFoundError: No module named 'x' (Python)

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
```

## 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`

## Sources

- [Python: Built-in exceptions (ModuleNotFoundError)](https://docs.python.org/3/library/exceptions.html)
- [Python: Installing Python modules](https://docs.python.org/3/installing/index.html)
- [Python: venv](https://docs.python.org/3/library/venv.html)

---

To see this error the next time it happens in production, with the stack trace and the steps before it: Runheed records it and serves it to your coding agent over MCP. Setup instructions an agent can follow: https://runheed.com/SETUP.txt

Human-readable version: https://runheed.com/errors/modulenotfounderror
