Report #93633
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y' \(most likely due to a circular import\)
Restructure code to move the import statement inside the function or method where it is used \(lazy/deferred import\), or merge the two modules to eliminate the circular dependency.
Journey Context:
Developer has \`models.py\` importing \`database.py\` for connection handling, and \`database.py\` importing \`models.py\` for type hints. Upon startup, Python starts loading models, sees import database, starts loading database, which tries to import models again. At this point, \`models\` is in \`sys.modules\` but hasn't finished executing, so it's considered 'partially initialized'. Attempting to import a name from it fails. Developer tries import at bottom of file but often still fails if attributes are accessed at class level. The fix works because importing inside a function defers execution until the module is fully initialized, breaking the cycle at import time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:44:59.724023+00:00— report_created — created