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.
What Does This Error Mean?
The 'ImportError: cannot import name' error occurs when Python finds the module you are importing from, but cannot find the specific name (function, class, or variable) you are trying to import from that module. This can happen due to circular imports, initialization order issues, or the name genuinely not existing in the module.
Common Causes
Circular import — two modules import from each other, causing one to be partially initialized
The name does not exist in the target module (typo or not yet defined)
Relative import failing in a script run directly (not as a module)
Name was removed or renamed in a newer version of the package
Imported module has its own import error that prevents full initialization
Name is defined conditionally (inside an if block) and the condition was False
How to Fix It
Check for typos in the import name
Verify the name exists in the target module and is spelled correctly.
# ❌ Typo
from datetime import datetim # ImportError
# ✅ Correct
from datetime import datetime
# Check what names a module exports
import datetime
print([x for x in dir(datetime) if not x.startswith("_")])Fix circular imports
Restructure code to break circular dependencies.
# ❌ Circular: module_a imports from module_b, module_b imports from module_a
# ✅ Solution 1: Move shared code to a third module
# shared.py: class BaseModel
# module_a.py: from shared import BaseModel
# module_b.py: from shared import BaseModel
# ✅ Solution 2: Lazy import inside a function (defers import until called)
# module_a.py
def get_user():
from module_b import User # Import only when function is called
return User.query.first()Run scripts as modules
Use python -m to run scripts that use relative imports.
# ❌ Direct execution fails with relative imports python app/utils/helper.py # ImportError: attempted relative import with no known parent package # ✅ Run as module python -m app.utils.helper # Project structure: # app/ # __init__.py # utils/ # __init__.py # helper.py
Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
ModuleNotFoundError: No module named
Fix 'ModuleNotFoundError: No module named' errors in Python. Learn to install missing packages, fix import paths, and manage virtual environments.
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.
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 a circular import and how do I detect it?
A circular import happens when module A imports from module B, and module B imports from module A (directly or indirectly). You can detect them by adding `print(f'Loading {__name__}')` at the top of each module to see the initialization order.
Can I use `import x` vs `from x import y` to avoid circular imports?
Using `import x` (module import) instead of `from x import y` (name import) can help because it defers name lookup. When you use `import x`, you access x.y later, after both modules are fully initialized. However, restructuring is the proper fix.