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.
- Print sys.executable from the failing environment to see which Python runs.
- Install with that interpreter: /path/to/python -m pip install <package>.
- Use the project venv's python path in cron jobs, service files and IDE run configs.
- Check PyPI for the distribution name when it differs from the import name.
- Add the package to requirements.txt or pyproject.toml so deploys install it.
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 landedPaste 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.
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
- TypeError: 'NoneType' object is not subscriptable
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
- Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
More in Imports and modules
- TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/index.tsNode.js
- Error: Cannot find module 'x'Node.js
- SyntaxError: Cannot use import statement outside a moduleNode.js
- Module not found: Can't resolve 'fs'Next.js
- Module not found: Can't resolve '@/components/OrderTable'Next.js
- ImportError: cannot import name 'x' from partially initialized module 'y' (most likely...Python
- ImportError: attempted relative import with no known parent packagePython
- AttributeError: module 'requests' has no attribute 'get'Python
- TypeError: 'module' object is not callablePython
- ImportError: libGL.so.1: cannot open shared object file: No such file or directoryPython
- Error: Could not load the "sharp" module using the linux-x64 runtimeNode.js
Other error topics
- Error: getaddrinfo ENOTFOUND api.example.comNetwork and HTTP
- ConnectionResetError: [Errno 104] Connection reset by peerNetwork and HTTP
- Error: 429 You exceeded your current quota, please check your plan and billing details.Network and HTTP
- psycopg.errors.UndefinedColumn: column "titel" does not existDatabases
- sqlite3.OperationalError: database is lockedDatabases
- psycopg2.OperationalError: server closed the connection unexpectedlyDatabases
- An error occurred in the Server Components render. The specific message is omitted in...React and Next.js rendering
- Error: NextRouter was not mounted.React and Next.js rendering
- Element type is invalid: expected a string (for built-in components) or a class/function...React and Next.js rendering
- TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.Undefined, None and type errors
- TypeError: unhashable type: 'list'Undefined, None and type errors
- panic: assignment to entry in nil mapUndefined, None and type errors
- pydantic_core._pydantic_core.ValidationError: 1 validation error for User email Field...JSON, parsing and validation
- TypeError: Converting circular structure to JSONJSON, parsing and validation
- json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)JSON, parsing and validation
- Error: ENOENT: no such file or directory, open '/app/config.json'Memory, async and the process
- django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.Memory, async and the process
- RuntimeWarning: coroutine 'fetch_user' was never awaitedMemory, async and the process
- Error: spawn npx ENOENTMCP servers and clients
- ModuleNotFoundError: No module named 'mcp.server.fastmcp'MCP servers and clients
- McpError: MCP error -32000: Connection closedMCP servers and clients
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.