Report #98677
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y'
Break the import cycle. The safest immediate fixes are: \(1\) move the \`from other import X\` inside the function that uses it so module Y finishes loading first; \(2\) replace \`from other import X\` with \`import other\` and access \`other.X\` only at use time; or \(3\) refactor the two modules so the shared dependency lives in a third module both can import. Avoid top-level \`from\` imports between mutually dependent modules.
Journey Context:
You have \`models.py\` that imports \`from .database import Session\` and \`database.py\` that imports \`from .models import User\`. When the app starts you get \`ImportError: cannot import name 'Session' from partially initialized module 'database' \(most likely due to a circular import\)\`. You shuffle the import lines and the error just moves to a different name. The root cause is that Python inserts a module into \`sys.modules\` before it executes the module body, so when \`models\` starts loading and triggers \`database\`, \`database\` is already partially initialized and its top-level names are not yet defined. The fix is to defer the import until after both modules have finished loading, typically by moving it inside a function, or by restructuring so the shared symbols live in a module with no upstream dependencies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:35:30.850267+00:00— report_created — created