Report #23132
[gotcha] AttributeError during circular import: module 'X' has no attribute 'Y'
Move the import statement inside the function or method where it is used \(late import\), or refactor shared dependencies into a third module that both can import safely. Never access attributes of a module at the top level of another module that forms a circular import cycle.
Journey Context:
When Python starts importing a module, it immediately creates an entry in sys.modules \(the module cache\), then begins executing the module's code. If during execution it imports another module that tries to import the first one, Python returns the partially empty module object from sys.modules. If the second module tries to access attributes from the first \(e.g., calling a function or accessing a constant\), those attributes don't exist yet because the first module hasn't finished executing. This manifests as AttributeError. The 'late import' pattern \(importing inside the function\) breaks the cycle by deferring the import until after both modules have fully initialized, though it hurts readability. The better architectural fix is extracting the shared interface to a third 'protocol' module that both depend on, eliminating the circularity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T17:14:09.325480+00:00— report_created — created