ModuleNotFoundError: No module named
Fix 'ModuleNotFoundError: No module named' errors in Python. Learn to install missing packages, fix import paths, and manage virtual environments.
What Does This Error Mean?
The 'ModuleNotFoundError: No module named' error occurs when Python's import system cannot find the module you are trying to import. The module is either not installed, not in the Python path, or the import statement has a typo or incorrect structure.
Common Causes
The package is not installed in the current environment
You are running code in the wrong virtual environment or no environment
The module name is misspelled in the import statement
The file is in a subpackage but the import path is missing the parent package
The package was installed for a different Python version (e.g., Python 2 vs 3)
Circular imports causing the module to be unavailable at import time
How to Fix It
Install the missing package with pip
Use pip to install the package in your current environment.
# Install the missing package pip install package-name # For Python 3 specifically pip3 install package-name # Install from requirements file pip install -r requirements.txt # Check installed packages pip list pip show package-name
Activate the correct virtual environment
Ensure you are using the virtual environment where the package is installed.
# Windows venv\Scripts\activate # macOS / Linux source venv/bin/activate # Check which Python is being used which python # Windows: where python # Verify packages in the active environment pip list
Fix PYTHONPATH for local modules
Add the parent directory to PYTHONPATH when importing local modules.
import sys import os # Add the project root to the path sys.path.append(os.path.dirname(os.path.dirname(__file__))) # Or set it before running # $env:PYTHONPATH = "C:\path\to\project" # PowerShell # export PYTHONPATH="/path/to/project" # bash
Check for circular imports
Restructure your code to avoid two modules importing each other.
# ❌ Circular import # module_a.py: from module_b import func_b # module_b.py: from module_a import func_a # ✅ Solution: move shared code to a third module # shared.py: shared constants and types # module_a.py: from shared import ... # module_b.py: from shared import ... # ✅ Or use lazy imports inside functions def run_task(): from module_b import func_b # Import only when needed func_b()
Before & After Examples
import requests # ModuleNotFoundError: No module named requests
# Install first
$ pip install requests
# Then import works
import requests
response = requests.get("https://api.example.com")Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
IndentationError: unexpected indent
Fix 'IndentationError: unexpected indent' in Python. Learn Python's indentation rules and how to fix spacing issues.
KeyError
Fix Python KeyError when accessing dictionary keys that do not exist. Learn to use .get(), setdefault(), and try-except safely.
ImportError: cannot import name
Fix 'ImportError: cannot import name' in Python. Learn why imports fail within modules and how to restructure your code to avoid circular dependencies.
AttributeError: 'NoneType' object has no attribute
Fix AttributeError: 'NoneType' object has no attribute errors in Python. Learn why functions return None and how to handle missing return values.
Frequently Asked Questions
What is the difference between ModuleNotFoundError and ImportError?
ModuleNotFoundError is a subclass of ImportError. ModuleNotFoundError means the module itself was not found. ImportError is broader — it can also mean a specific name was not found within a module (e.g., `from x import y` where y does not exist in x).
Why does pip install work but Python still cannot find the module?
This usually means pip installed the package for a different Python version than the one running your script. Check with `pip --version` and `python --version` to ensure they match. Use `python -m pip install package-name` to guarantee the correct Python is targeted.