Python ModuleNotFoundError: No module named … (but I installed it!)

Python looks for modules in the folders on sys.path of the interpreter that is running. Almost every “but I installed it” case is pip putting the package into a different Python than the one running your code.

Step 1: which Python is running, and where did pip install?

python -c "import sys; print(sys.executable)"
python -m pip --version        # shows pip's Python and its site-packages folder
pip --version                  # bare pip may belong to a different Python
python -m pip show requests    # Location: tells you where it really went

If the paths disagree (say /usr/bin/python3 versus ~/project/.venv/bin/python), that’s your bug. The cure is always the same: run python -m pip install … with the exact python that runs your code.

The usual causes

SituationFix
Virtual environment not activated (installed globally, running in the venv, or the other way round)source .venv/bin/activate (Windows: .venv\Scripts\activate), then python -m pip install …
Several Pythons (system, Homebrew, pyenv, Windows Store, conda)Use one explicitly: python3.12 -m pip install … and run with python3.12
VS Code runs a different interpreterCommand Palette, “Python: Select Interpreter”, pick the .venv one
Jupyter kernel isn’t your venvIn a cell: %pip install requests (installs into the running kernel)
sudo pip install or sudo python script.pysudo can switch to root’s Python and path; use a venv and no sudo
Package name ≠ import nameSee the table below
Your file is called requests.py, random.py, yaml.py…Rename it and delete __pycache__; Python imported your file instead
Cron, systemd, DockerThey don’t load your shell or venv; use the full path /path/.venv/bin/python
Your own package: No module named 'app'Run from the project root with python -m app.main, or pip install -e .; don’t hack sys.path

Install name vs import name

You importYou install
cv2opencv-python
yamlPyYAML
PILPillow
sklearnscikit-learn
bs4beautifulsoup4
dotenvpython-dotenv
jwtPyJWT
dateutilpython-dateutil

Never pip install a guessed name blindly: typo-squatted packages with near-identical names exist. Check the project page first.

“externally-managed-environment” instead?

Newer Debian, Ubuntu and Homebrew Pythons refuse system-wide pip installs (PEP 668). That is the hint to make a venv:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

For command-line tools, pipx install <tool> gives each one its own environment. Avoid --break-system-packages; it can break the operating system’s own Python scripts.

See exactly where Python is looking

python -c "import sys; print('\n'.join(sys.path))"
python -c "import requests; print(requests.__file__)"   # once it works

Comparing sys.path from your terminal and from the failing place (IDE, cron, CI) usually shows the difference at a glance; the text diff tool helps. Keep secrets out of those environments too: see .env files and secrets.

pip itself refusing with error: externally-managed-environment? See externally-managed-environment fixed.

On Windows, can’t even activate the venv? See running scripts is disabled in PowerShell. Activated the venv but which python is still the system one? See Python venv not activating / which python.

Related

ResolutionImpossible · Failed building wheel · ERR_EMPTY_RESPONSE · Cloudflare Error 525 · Cloudflare Error 526 · Module not found: Can't resolve · pip: Could not find a version · Error: Cannot find module (Node)

Questions

Why does Python say no module named after pip install?

pip installed the package for a different Python than the one running your code, usually because a virtual environment isn't activated or your editor uses another interpreter. Use python -m pip install package with the same python you run.

What is the difference between pip install and python -m pip install?

Bare pip is whichever pip script comes first on your PATH, which may belong to another Python. python -m pip always uses the pip of that exact python, so the package lands where your code will look.

How do I fix ModuleNotFoundError in Jupyter?

Run %pip install package-name in a notebook cell. It installs into the kernel that is actually running the notebook, unlike !pip, which may use a different Python.

Why is cv2 or yaml not found after installing?

Some packages are installed under a different name from the one you import: cv2 comes from opencv-python, yaml from PyYAML, PIL from Pillow, sklearn from scikit-learn.

Published · updated .

Try it

Related guides

Newest guides