ImportError: Circular Import
ImportError: cannot import name 'B' from partially initialized module 'b' (most likely due to a circular import)Quick Answer
Two modules are importing each other, creating a circular dependency. Break the cycle by moving the import inside the function that needs it, or restructure your code.
Why This Happens
When module A imports module B and module B imports module A, Python gets stuck in a loop. Module A starts loading, triggers loading of B, which tries to import from the still-loading A. Since A is not fully initialized, the name may not exist.
The Problem
# a.py
from b import B
class A: pass
# b.py
from a import A
class B: passThe Fix
# a.py
class A: pass
# b.py
class B:
def use_a(self):
from a import A
return A()Step-by-Step Fix
- 1
Identify the cycle
Trace the import chain to find the circular dependency.
- 2
Move imports inside functions
Delay the import until the function is called.
- 3
Restructure the code
Extract shared code into a third module.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free