Blog
3 min read

Python "ModuleNotFoundError: No module named ...": How to Fix It

Python can't find a module you imported. Usually the package isn't installed in the environment you're running, you installed it into a different Python, the package name differs from the import name, or it's your own module and the path is wrong. How to check and fix each.

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

Python looked for a module called requests and couldn't find it in any of the places it searches. Nine times out of ten, the package is installed — just not in the Python that's running your code.

Cause 1: it isn't installed

The simple case. Install it:

python -m pip install requests

Using python -m pip (rather than plain pip) guarantees you install into the same Python you'll run with. On many Macs and Linux systems the command is python3.

If the project has a requirements.txt, install everything at once:

python -m pip install -r requirements.txt

(requirements.txt explained)

Cause 2: wrong environment (the most common)

You have more than one Python: the system one, one from Homebrew or the Microsoft Store, and probably a virtual environment for this project. You installed the package into one and are running another. (Python virtual environments)

Check which Python is running and which pip installs where:

python -c "import sys; print(sys.executable)"
python -m pip --version

Activate the project's virtual environment

source .venv/bin/activate     # macOS / Linux
.venv\Scripts\activate        # Windows

Your prompt shows (.venv) when it's active. Then install and run again.

Your editor uses a different interpreter

VS Code picks a Python interpreter per workspace. If running from the terminal works but the editor's Run button fails (or the reverse), open the Command Palette → Python: Select Interpreter and choose the project's .venv. (VS Code for beginners)

Jupyter uses a different kernel

In a notebook, install into the kernel's environment:

%pip install requests

Cause 3: the install name differs from the import name

Some packages are installed under one name and imported under another:

import pip install
cv2 opencv-python
PIL pillow
sklearn scikit-learn
yaml pyyaml
bs4 beautifulsoup4
dotenv python-dotenv

pip install cv2 fails or installs the wrong thing. Look up the correct package name.

Be careful with AI-suggested package names too — models sometimes invent plausible-sounding packages, and attackers register those names. Check the package exists and is the real one before installing. (AI hallucinated packages)

Cause 4: it's your own module

ModuleNotFoundError: No module named 'utils'

when utils.py is in your project. Python finds your own modules based on where you run from, not where the file is.

  • Run from the project root: python -m app.main rather than cd app && python main.py.
  • Make sure folders that are packages are importable (a folder of modules imported as app.utils should be inside the directory you run from).
  • Avoid naming your files after real packages — a file called requests.py or email.py shadows the real one and causes confusing errors.

(File paths explained)

Cause 5: it works locally but not on the server

The server's environment doesn't have the package because it wasn't in requirements.txt. Freeze what you've installed:

python -m pip freeze > requirements.txt

…or better, add packages explicitly as you go. (Works locally but not in production)

And if pip itself refuses

On newer Linux and Homebrew Pythons, pip install outside a virtual environment fails with externally-managed-environment. That's a nudge to use a virtual environment. (Fix it)


EasySpawn runs your Python app in a consistent environment on its own server, with dependencies installed from your requirements on every deploy. See how it works or join the waitlist.

Related: Python Virtual Environments · requirements.txt Explained · pip "externally-managed-environment" · What Is Python?

Keep reading