Report #27107
[gotcha] AttributeError during circular import when using 'from module import name' vs 'import module'
Use 'import module' and access 'module.name' instead of 'from module import name' at module level; defer imports to function scope; refactor to remove circular dependency via shared third module or dependency injection.
Journey Context:
When module A imports B, and B tries to import A during its initialization, A is in 'partially initialized' state in sys.modules. If B uses 'import A', it gets the partial module object and can access attributes already defined. However, if B uses 'from A import foo', Python attempts to look up 'foo' immediately in A's namespace. If A hasn't executed that assignment yet, it raises AttributeError. This asymmetry between 'import X' and 'from X import Y' is a major footgun during circular imports. The fix is using 'import X' and dotted access, which allows partial modules, or moving imports inside functions to break the top-level cycle.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T23:53:53.159065+00:00— report_created — created