ImportError covers failures where Python cannot complete an import request. That can mean the module name is wrong, a dependency is missing, the package is not installed in the active environment, or the module does not export the name being requested.
This page should explain how import resolution follows module names, package layout, and the current environment, because the error often comes from a mismatch between what is installed and what the interpreter is actually using.
The most useful debugging angle is to check the active interpreter, the install location, and the exact import statement separately instead of treating all import failures as the same problem.
An ImportError occurs in Python when an import statement fails to find or load a module. The most common subclass is ModuleNotFoundError (Python 3.6+), which specifically means the module does not exist in the current environment. This error typically happens when a package is not installed, the module name is misspelled, or the Python environment is incorrect.
# Missing dependency: requests is not installed here
import requests # ImportError: No module named 'requests'
# Install the package in the active environment
# pip install requests
# Then import works:
import requests
response = requests.get("https://api.example.com/data")
Third-party packages like requests, numpy, or pandas are not part of Python's standard library. You must install them with pip before importing them.
Module names are case-sensitive and must be spelled exactly. The package name you install with pip is sometimes different from the module name you import (e.g., pip install Pillow but import PIL).
A circular import occurs when module A imports module B, and module B also imports module A. Python partially loads modules, so the second import may fail to find names that haven't been defined yet.
When using virtual environments, a package installed in one environment is not available in another. This is a common issue when switching between projects or when an IDE uses a different Python interpreter than the terminal.
import numpy as np # ModuleNotFoundError: No module named 'numpy'
import pandas as pd # ModuleNotFoundError: No module named 'pandas'
# Install via pip in your terminal
# pip install numpy pandas
# Or install from requirements.txt
# pip install -r requirements.txt
# Verify installation
# pip show numpy
import numpy as np # Works after installation
import pandas as pd # Works after installation
import Requests # ModuleNotFoundError (should be lowercase 'requests')
import sklearn # ModuleNotFoundError (pip install scikit-learn, import sklearn)
import cv2 # ModuleNotFoundError (pip install opencv-python, import cv2)
import requests # lowercase
# pip install scikit-learn
import sklearn # import name differs from pip name
# pip install opencv-python
import cv2 # import name differs from pip name
# pip install Pillow
from PIL import Image # import PIL, not Pillow
# module_a.py
from module_b import func_b # ImportError: cannot import name 'func_b'
def func_a():
return "A"
# module_b.py
from module_a import func_a # Circular import!
def func_b():
return func_a() + "B"
# Break the import cycle by moving shared code to module_common.py
# module_common.py
def func_a():
return "A"
# module_b.py
from module_common import func_a
def func_b():
return func_a() + "B"
# Delay the import when restructuring is not possible
# module_a.py
def func_a():
from module_b import func_b # Import only when needed
return func_b()
# Installed requests in venv1, but running script with venv2 or system Python
# pip install requests (in venv1)
# python script.py (using system Python - no requests!)
import requests # ModuleNotFoundError
# Activate the correct virtual environment first
# Windows: venv\Scripts\activate
# macOS/Linux: source venv/bin/activate
# Verify which Python you're using
import sys
print(sys.executable) # Shows the Python interpreter path
# Install in the active environment
# python -m pip install requests (uses the active Python's pip)
ModuleNotFoundError is the ImportError subtype raised when Python cannot locate the requested module. ImportError is broader and also covers failures such as importing a missing name from a module that was found.
Run pip install package-name in your terminal. Make sure your virtual environment is activated first. Use python -m pip install package-name to ensure you install into the correct Python environment.
You likely installed the package in a different Python environment than the one running your script. Check which Python is active with python --version and which pip is used with pip --version. They should point to the same environment.
Explore 500+ free tutorials across 20+ languages and frameworks.