ErrorPython

ModuleNotFoundError: No module named

Why Python's ModuleNotFoundError happens even when a package looks installed, how mismatched environments cause it, and how to fix each cause.

Last updated

ErrorPython
ModuleNotFoundError: No module named 'requests'

What it means: Python tried to import a module by that name and couldn't find it anywhere on `sys.path` — it isn't installed where this interpreter looks, or the name itself is wrong.

Likely causes

Most probable first — with how to confirm.

  • 1.The package isn't installed in the environment you're actually running — check `pip show requests`; and especially check you're not in a different virtualenv or conda env than the one you installed into (`which python` or `where python`).
  • 2.You installed it with a different Python/pip than the one executing the script — on systems with multiple Pythons, `pip install` and `python script.py` can silently point at different interpreters.
  • 3.It's a local module, and Python can't find it because you ran the script from the wrong directory, the package is missing an `__init__.py` on an older Python version, or it isn't on `PYTHONPATH`.
  • 4.You mistyped the import name or confused the PyPI package name with the actual import name — most packages match, but a few well-known ones (like `python-dotenv`, imported as `dotenv`) don't.

Fixes

Safest first; destructive ones are called out.

  • →Install into the environment you're actually running: activate the right virtualenv or conda env first, then run `python -m pip install requests` so it definitely targets the interpreter you'll execute the script with.
  • →Check `python -c "import sys; print(sys.executable)"` and `print(sys.path)` to see exactly which interpreter and search path are in play, especially in mixed Python 2/3 or multi-venv setups.
  • →For local or first-party modules, run the script from the project root, or use `python -m package.module`, so relative imports resolve; add an `__init__.py` if the package needs one.
  • →Double-check the import name against the package's own documentation — the install name and the import name diverge for a fair number of common packages.

Part by part

ModuleNotFoundError
A subclass of ImportError, added in Python 3.6 specifically for the case where the module was not found at all — as opposed to found and failing to import.
No module named
Followed by the name Python looked for. It searched every entry in sys.path and found nothing matching.
'requests'
The module name as written in your import. A typo here fails identically to a genuinely missing package, so read it before installing anything.

Common mistakes

  • pip install requests

    Correct only if pip belongs to the interpreter that raised the error. With several Pythons installed, pip frequently points at a different one, and the package installs somewhere your script never looks.

    python -m pip install requests
  • sudo pip install requests

    Installs into the system Python, which the operating system also depends on. It can break system tooling and still not affect your virtual environment, so it manages to be both risky and ineffective.

  • Adding the directory to sys.path at runtime

    A workaround that makes the script work from one location and fail from another. It also hides a packaging problem that will resurface the moment anyone else runs the code.

Questions

How do I confirm which interpreter is running?

Print sys.executable from inside the failing script. That is the definitive answer, and comparing it with which python or where python explains most of these errors immediately.

Why does python -m pip differ from pip?

python -m pip guarantees the pip belonging to that exact interpreter. A bare pip is whichever appears first on your PATH, which on a machine with several Pythons is frequently not the one you are running.

Is my virtual environment actually active?

Check for VIRTUAL_ENV in the environment, or look at sys.prefix. An environment created but not activated is the single most common cause, and the prompt is not always a reliable indicator.

What if the module is installed and still not found?

Then it is usually a name mismatch — the distribution and the import name differ often, as with the beautifulsoup4 package importing as bs4. Check the project's documentation for the import name rather than assuming.

Related in Errors