Error Encyclopedia

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

1

Circular import — two modules import from each other, causing one to be partially initialized

2

The name does not exist in the target module (typo or not yet defined)

3

Relative import failing in a script run directly (not as a module)

4

Name was removed or renamed in a newer version of the package

5

Imported module has its own import error that prevents full initialization

6

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:

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.