Python ImportError vs ModuleNotFoundError: What's the Difference and How to Fix Both
ModuleNotFoundError and ImportError look similar but have completely different causes and fixes. One means Python can't find the module at all. The other means it found the module but can't find what you're importing from it. This guide covers every cause and fix for both, including circular imports, wrong environments, and package name mismatches.
The Difference
ModuleNotFoundError means Python cannot find the module at all. It is either not installed, not on the Python path, or the name is wrong.
ImportError means Python found the module but cannot import a specific name from it. The module exists, but the thing you are trying to import from it does not.
Both have different causes and different fixes.
Fixing ModuleNotFoundError
Step 1: Check if it's installed
If there is no output, it is not installed. Install it:
Step 2: Check you're in the right environment
If they point to different paths, you installed into the wrong environment. Activate your virtualenv:
Step 3: Always use python -m pip
This guarantees installation into the Python that is actually running your code.
Step 4: Package name vs import name
Some packages have different install names and import names. The most common ones:
| Install name | Import name |
|---|---|
| Pillow | PIL |
| scikit-learn | sklearn |
| python-dateutil | dateutil |
| opencv-python | cv2 |
| beautifulsoup4 | bs4 |
Fixing ImportError
cannot import name 'X' from 'Y' has three causes.
1. Name Does Not Exist in the Module
The function may have been renamed, removed, or moved in a newer version. Check the module's documentation or source:
Verify the version matches the docs you are reading.
2. Circular Import
Module A imports from Module B. Module B imports from Module A. Python cannot resolve either one.
Move shared code to a third module that neither A nor B imports from.
3. Importing From the Wrong Location
The name exists in the package but at a different path than you are using. Check the official docs for the correct import path.
For Local Module ImportErrors
If the error is about your own code:
- Check you have
__init__.pyin each package directory - Run scripts from the project root, not from inside subdirectories
- Install your package in editable mode:
pip install -e .
Your project structure should look like this:
Finding the Root Cause Fast
Import errors usually have clear messages. When they happen deep in a dependency chain where one module imports another that imports another that fails, the stack trace gets long and confusing.
DebugAI reads your import graph when you index your project. When an import error hits, it already knows your module structure and can tell you exactly which import is failing and why, without you manually tracing the chain.
Open Command Palette with Ctrl+Shift+P and run DebugAI: Analyze Terminal Error.
FAQ
Q: I installed the package but still get ModuleNotFoundError. What now?
A: Run python -m pip show package-name. If it shows up, but the error persists, your script is running under a different Python than the one where you installed it. Use python -m pip install to guarantee they match.
Q: How do I find out what names a module actually exports?
A: Run python -c "import module_name; print(dir(module_name))" in your terminal. This lists everything the module exposes. If the name you are importing is not in that list, it does not exist in the installed version.
For import errors that happen deep in a dependency chain, paste the full stack trace into DebugAI. It reads your import graph and identifies exactly which import is failing and why.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.