ModuleNotFoundError: No module named 'x' means Python could not find a module you tried to import. For standard library modules this usually means a typo or wrong Python version. For third-party modules (e.g. requests, numpy) it almost always means the package is not installed in the Python environment you are using. This guide covers causes and fixes.
What this error means
When you run import foo, Python looks for a module named foo in the search path (built-in modules, standard library, and directories in sys.path). If no matching module or package is found, Python raises ModuleNotFoundError: No module named 'foo'. For “cannot import name …” from a module that is found, see ImportError.
Common causes
- Package not installed. Third-party packages must be installed with
pip install <package>(or similar) in the same environment you use to run the script. - Wrong environment. You installed the package in one Python or virtual environment but are running the script with another. The interpreter that runs the code must be the one that has the package.
- Misspelled name. The import name must match the package or module name (e.g.
pip install Pillowbutimport PIL). - Local file shadowing. A file or folder in your project has the same name as a standard or third-party module, so Python loads that instead and may then fail.
Example that causes the error
import requests # ModuleNotFoundError if requests not installed
import numpy as np # Same if numpy not installed
Fix 1: install the package with pip
In the same environment you use to run your script, install the missing package:
# In terminal (same env as your script):
pip install requests
pip install numpy
# Then in code:
import requests
import numpy as np
Fix 2: make sure you use the right Python and pip
If you have multiple Python versions or virtual environments, use that environment’s pip and python:
# See which Python runs your script:
python -c "import sys; print(sys.executable)"
# Install for that Python explicitly:
python -m pip install requests
# Check installed packages:
pip list
python -c "import requests" # Fails if not installed
Fix 3: use a virtual environment
Create and activate a virtual environment, then install packages there so the correct interpreter and packages are used together.
# Create and activate (Windows):
python -m venv myenv
myenv\Scripts\activate
# Install:
pip install requests
Next steps: Errors & Exceptions · ImportError · Libraries