Debugging — Python Error Guide

ModuleNotFoundError —
Python looked, and couldn't find it.

Python searches a specific list of locations for a module, in a fixed order, and if the name you imported isn't found in any of them, this is the result. It's almost always a setup issue — not installed, wrong name, or wrong environment — not a bug in your logic. Watch Python's module search path get checked on LearnBug and see exactly where it comes up empty.

ModuleNotFoundErrorException type
sys.pathWhere Python looks
PythonLanguage

What is ModuleNotFoundError?

Raised when Python's import statement can't locate the named module anywhere in sys.path — a specific, ordered list of directories Python searches, including your current script's directory, installed site-packages, and any directories listed in the PYTHONPATH environment variable.

Why visualization helps

Unlike most errors on this hub, this one isn't really about code logic — it's about environment. LearnBug can't fix a missing package install for you, but understanding exactly what Python checked and in what order turns a confusing "why can't it find this" moment into a clear, mechanical troubleshooting checklist.

LearnBug — the module search path, checked in order
🖼 Add a LearnBug screenshot here
What Causes It

Three reasons Python can't find your import

Package not installed

Third-party packages (like requests, numpy, pandas) don't come with Python by default — they must be installed with pip first.

pip install

Installed in a different Python environment

A package installed in one virtual environment (or a different Python version) isn't visible to a script running under a different environment.

Wrong venv

Wrong module name

Some packages have a different install name than import name — pip install beautifulsoup4 but import bs4, for example.

Name mismatch
Walkthrough

import requests without it installed

1

import requests triggers Python's module search

Python checks a specific ordered list of locations: the script's own directory, then built-in standard library modules, then installed third-party packages in site-packages.

script dir
standard library
site-packages
Searching sys.path for "requests" in order
2

No requests.py in the script's directory. No "requests" in the standard library either

requests is a third-party package — it was never part of Python's built-in standard library, so this check always fails for it unless it's specifically installed.

script dir: not found
stdlib: not found
Two locations checked: neither has "requests"
3

site-packages is also checked — requests was never pip installed, so it's not there either

This is the location where pip install would have placed it. Since that command was never run in this environment, the package genuinely doesn't exist anywhere Python looks.

Result: ModuleNotFoundError: No module named 'requests'

Code Example

The broken code, and the traceback it produces

PythonBroken — requests was never installed
import requests   # not installed in this environment

response = requests.get("https://example.com")
print(response.status_code)
TracebackWhat Python actually prints
Traceback (most recent call last):
  File "fetch.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
The Fix

Install the package into the environment you're running

ShellInstall with pip, then re-run
# Install the package (in the same environment your script runs in)
pip install requests

# If you use multiple Python versions, be explicit:
python3 -m pip install requests

# If using a virtual environment, activate it FIRST, then install:
source venv/bin/activate
pip install requests

Quick Reference

Error classModuleNotFoundErrorA subclass of ImportError, specifically for "module not found at all"
Typical causeNot installed / wrong environmentA setup issue, not a code logic bug
Fix patternpip install <package>Run it in the same environment/interpreter your script actually uses
Verify environmentwhich python / pip listConfirm you're installing into the same Python your script runs with, especially with virtual environments

Frequently asked questions

I already ran pip install — why is it still not found?

Almost always an environment mismatch — you installed the package into a different Python interpreter or virtual environment than the one actually running your script. Check with which python (or where python on Windows) that both the install command and your script are using the same interpreter.

Why is my import name different from what I typed in pip install?

A package's PyPI distribution name (what you type after pip install) doesn't have to match its Python import name — beautifulsoup4 is imported as bs4, and Pillow is imported as PIL, for example. Check the package's documentation for the correct import name if installing didn't seem to fix it.

What's the difference between ModuleNotFoundError and ImportError?

ModuleNotFoundError is a more specific subclass of ImportError, introduced to distinguish "the module doesn't exist at all" from other import failures — like a module that exists but fails partway through its own initialization code, or a specific name you tried to import from inside a module that isn't actually defined there.

Why does it work on my machine but not on a teammate's / a server?

Each machine or environment has its own independently installed set of packages — installing something locally doesn't install it anywhere else. Use a requirements.txt file (generated with pip freeze) and have every environment run pip install -r requirements.txt to keep dependencies consistent across machines.

See exactly what Python could and couldn't find

Paste your import statements into LearnBug to check for other bugs once your environment is set up.

Open Playground →