Skip to main content

Python · 5 common causes

ModuleNotFoundError: No module named

Updated

The interpreter that ran your code cannot see that package on its import path.

The exact message

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

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ModuleNotFoundError: No module named.

How to fix it

Cause 1 of 5

The package is installed for a different interpreter than the one running.

Install into the exact interpreter that runs your code: python -m pip install requests. The python -m pip form binds pip to that Python; a bare pip on PATH may belong to another installation entirely.

Cause 2 of 5

You are outside the virtual environment.

Activate it first — source .venv/bin/activate, or .venv\Scripts\activate on Windows — then install. In VS Code, also select the venv interpreter in the status bar, because the Run button and the integrated terminal can otherwise use two different Pythons.

Cause 3 of 5

The install name differs from the import name.

pip install beautifulsoup4 imports as bs4, pip install pillow imports as PIL, pip install opencv-python imports as cv2, pip install python-dateutil imports as dateutil. Check the project's README for the import name rather than guessing from the package name.

Cause 4 of 5

Your own module is not on sys.path.

Running python subdir/script.py puts subdir/ on the path, not the project root, so sibling packages become invisible. Run it as a module from the root instead — python -m package.script — or install the project in editable mode with pip install -e ..

Cause 5 of 5

In Docker, the install layer was cached with an older requirements file.

Rebuild that layer: docker compose build --no-cache <service>. Copying requirements.txt before the rest of the source is the ordering that makes the cache invalidate correctly when dependencies change.

Stopping it coming back

Use one virtual environment per project, install with python -m pip, and keep requirements.txt or pyproject.toml as the single record of what the project needs.

ModuleNotFoundError: No module named — common questions

How do I tell which Python is actually running?
Print it from inside the failing program: import sys; print(sys.executable, sys.path). That is the interpreter you must install into, and the path list shows exactly where it looked.
Why does it work in the terminal but not in my IDE?
The IDE is using a different interpreter. In VS Code, click the Python version in the status bar and pick the one at .venv/bin/python; in PyCharm, set the project interpreter to the venv. The mismatch is the single most common cause of this error.
What is the difference between ModuleNotFoundError and ImportError?
ModuleNotFoundError is a subclass of ImportError, raised when the module itself cannot be located. A plain ImportError means the module was found but a specific name inside it could not be imported — which usually points at a circular import or a version mismatch.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor