Report #15379
[gotcha] Partially initialized module imported during circular import
Avoid side effects and attribute access at module import time \(top-level\) in modules that participate in circular imports. Move imports inside functions \(lazy import\) or move shared code to a third module imported by both. If you must have circular imports, ensure the imported module doesn't access attributes of the importing module during its initialization. Use \`if \_\_name\_\_ \!= '\_\_main\_\_':\` guards only for execution, not for import avoidance.
Journey Context:
When module A imports B, and B \(during its load\) imports A, Python returns the partially initialized module A from \`sys.modules\` to B. If B then tries to access \`A.some\_function\` \(defined later in A\), it raises \`AttributeError: partially initialized module 'A' has no attribute 'some\_function'\`. This is insidious because the error appears in B, but the cause is the order of definitions in A. The fix is architectural: eliminate circular dependencies or defer them to runtime \(inside functions\), rather than trying to reorder imports, which is fragile.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:53:00.868067+00:00— report_created — created