Report #36636
[bug\_fix] ImportError: cannot import name 'Foo' from partially initialized module 'bar' \(most likely due to a circular import\)
Restructure code to avoid circular dependencies \(e.g., move shared code to a third module\), or move the import statement inside the function/method where it is used \(lazy/deferred import\), or use \`typing.TYPE\_CHECKING\` for type hints only.
Journey Context:
Developer has \`models.py\` importing \`from .schemas import Foo\` and \`schemas.py\` importing \`from .models import Bar\`. When they try to run the app, Python starts loading \`models\`, which triggers loading \`schemas\`, which tries to load \`models\` again. At this point, \`models\` is in \`sys.modules\` but is only partially initialized \(hasn't finished executing\), so the import system raises \`ImportError\`. Developer tries moving the import to the bottom of the file, which can work if the names aren't needed at class definition time, but is fragile. The robust solution is to move the common definitions to a new \`types.py\` or \`constants.py\` that both can import, breaking the cycle. Alternatively, if the import is only needed for type hints, they use \`from typing import TYPE\_CHECKING\` and \`if TYPE\_CHECKING: from .models import Bar\` to avoid runtime import. The fix works because it breaks the circular dependency at import time, ensuring modules are fully initialized before their contents are accessed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T15:58:23.659986+00:00— report_created — created