Report #14195
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y' \(most likely due to a circular import\)
Move the import statement inside the function/method where it is used \(lazy import\), or restructure modules to break the circular dependency by merging modules or introducing a third shared module.
Journey Context:
Developer has \`models.py\` importing \`utils.py\` for a helper function, and \`utils.py\` importing \`models.py\` to type-check or use a class defined there. When running the application, Python starts loading \`models.py\`, executes the top-level import of \`utils\`, switches to loading \`utils.py\`, which then tries to import from \`models\`. But \`models\` is only partially initialized \(in \`sys.modules\` but not finished executing\), so Python raises \`ImportError: cannot import name 'MyModel' from partially initialized module 'models'\`. The developer tries reordering imports or using \`import models\` instead of \`from models import MyModel\`, but the circularity remains. The "aha" moment comes from realizing that the import at the module level happens at import time, creating a hard dependency. The fix is to move the \`from models import MyModel\` inside the function in \`utils.py\` that actually uses it, deferring the import until runtime when both modules are fully initialized. Alternatively, restructuring to have both classes in a separate \`types.py\` that both import solves it architecturally.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:51:18.028781+00:00— report_created — created