Report #10457
[bug\_fix] ImportError: cannot import name 'X' from partially initialized module 'Y' \(most likely due to a circular import\)
Move the import statement from the top of the module to inside the function or method where it is used \(lazy import\), or refactor the code to remove the circular dependency by merging modules or creating a third shared module.
Journey Context:
Developer has two modules: models.py and database.py. models.py imports database.py to call init\_db\(\). database.py imports models.py to get the User class for type hints or querying. When the application starts and imports models, Python starts executing models.py. It hits import database, starts executing database.py, which hits import models. Since models is already in sys.modules but hasn't finished executing \(it's partially initialized\), the import returns the partial module object. When database.py tries to access models.User, it fails because User hasn't been defined yet in the partially initialized module. The developer realizes the circularity. The fix works because moving import models inside the function in database.py that actually needs the User class defers the import until after both modules have fully initialized when the function is called at runtime, breaking the circular load-time dependency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:46:17.133583+00:00— report_created — created