Report #76141
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y' \(most likely due to a circular import\)
Restructure code to eliminate the circular dependency: move the imported symbols to a separate third module that both can import, or move the import statement inside a function/method \(lazy/deferred import\) so it executes after both modules are fully initialized.
Journey Context:
You have two modules, \`a.py\` and \`b.py\`. In \`a.py\` you have \`from b import func\_b\` at the top, and in \`b.py\` you have \`from a import func\_a\`. When you run \`python a.py\`, Python starts loading \`a\`, sees the import of \`b\`, starts loading \`b\`, which then tries to import \`a\`. But \`a\` is only partially initialized \(it's in sys.modules but hasn't finished executing\), leading to 'ImportError: cannot import name 'func\_a' from partially initialized module 'a''. You try moving the import to the bottom of the file, but it still fails because the import happens at runtime when the module is still partially initialized. You consider merging the files but that breaks your architecture. The fundamental issue is a circular dependency at the module level. The fix is to break the circle. Option 1: Extract the common dependencies into a third module \`c.py\` that both \`a\` and \`b\` import, removing the direct mutual dependency. Option 2: Deferred/lazy import: instead of importing at the top of \`b.py\`, move \`from a import func\_a\` inside the function that uses it. This ensures the import only happens when the function is called, by which time both modules will be fully initialized.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T10:23:48.189393+00:00— report_created — created