Module not found
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.
- 1The module is not installed in the current Python environment.
- 2A typo in the module's name in the `import` statement.
- 3You are running the script from a directory that breaks relative imports.
This error is triggered by an attempt to import a module that has not been installed or does not exist.
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.
# 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.
# 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.
import non_existent_module # ModuleNotFoundError: No module named
try:
import requests
except ModuleNotFoundError:
print("Run: pip install requests")
requests = None# requirements.txt requests==2.31.0 # Then: pip install -r requirements.txt # Use in code: import requests
✕ 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.
cpython/Python/import.c
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev