A ModuleNotFoundError is a subclass of ImportError raised when Python cannot locate a module at all. Unlike ImportError (which can also fire when a specific name inside a module is missing), this error means the entire module was not found on sys.path.
Why it happens
- •The package is not installed in the active virtual environment.
- •The script is running in a different Python environment than the one where the package was installed.
- •A typo in the module name.
- •A local file has the same name as a standard library module, shadowing it.
Examples
Code that triggers the error
import pandas as pd
df = pd.read_csv("data.csv")Error output
Traceback (most recent call last):
File "app.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'How to fix it
Activate the correct virtual environment and run pip install <package>. Verify which Python interpreter is active with python --version and which pip you are using with pip --version. Check for naming conflicts between your local files and standard library modules.
Fixed code
# Install the missing package first:
# pip install pandas
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head())Related errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free