ModuleNotFoundError
PythonERRORRuntime ErrorHIGH confidence

Module not found

What this means

A subclass of `ImportError`, raised specifically when a module could not be located by the import system. This means Python looked for the module's file but could not find it anywhere it searched.

Why it happens
  1. 1The module is not installed in the current Python environment.
  2. 2A typo in the module's name in the `import` statement.
  3. 3You are running the script from a directory that breaks relative imports.
How to reproduce

This error is triggered by an attempt to import a module that has not been installed or does not exist.

trigger — this will error
trigger — this will error
import non_existent_module

expected output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'non_existent_module'

Fix 1

Install the module with a package manager

WHEN When trying to use a third-party library that isn't installed.

Install the module with a package manager
# In your terminal, not in a Python script:
pip install requests

Why this works

Package managers like `pip` download the module from the Python Package Index (PyPI) and place it where your Python interpreter can find it.

Fix 2

Correct the typo in the module name

WHEN You have misspelled the module's name.

Correct the typo in the module name
# Incorrect: import datetim
# Correct:
import datetime

Why this works

Correcting the spelling to match the actual module name allows Python's import machinery to locate and load it.

Code examples
Triggerpython
import non_existent_module  # ModuleNotFoundError: No module named
Handle with try/exceptpython
try:
    import requests
except ModuleNotFoundError:
    print("Run: pip install requests")
    requests = None
Avoid with requirements.txtpython
# requirements.txt
requests==2.31.0
# Then: pip install -r requirements.txt
# Use in code: import requests
What not to do

Manually copying library files into your project directory

This is not a scalable or maintainable way to manage dependencies. It makes updates difficult and can lead to versioning conflicts.

Version notes

Sources
Official documentation ↗

cpython/Python/import.c

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors