Report #97717
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y' \(most likely due to a circular import\)
Restructure the code so the module imported at the top of \`Y.py\` does not need to import \`Y\` during import time. Typical fixes: \(1\) move the imported symbol into a third module both can import, \(2\) delay the import inside the function/method that uses it, or \(3\) reorder definitions. The root cause is that during \`import Y\`, Python executes \`Y.py\` top-to-bottom; when a statement inside \`Y.py\` imports \`X\` from another module that transitively imports \`Y\`, \`Y\` is only partially initialized and the requested name has not been bound yet.
Journey Context:
You add a new constant to \`utils.py\` and import it in \`models.py\`, which is imported by \`app.py\`. Suddenly \`python app.py\` crashes with the circular import error. You print the traceback and see \`app.py -> models.py -> utils.py -> models.py\`. You try importing at the bottom of \`utils.py\`, which works until another entry point triggers the same cycle. You then extract the shared constant into a tiny \`constants.py\` that has no downstream imports; both \`models.py\` and \`utils.py\` import from \`constants.py\`, breaking the cycle because the leaf module does not re-import its consumers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:34:52.405225+00:00— report_created — created