Report #15130
[bug\_fix] Circular import causing AttributeError: partially initialized module
Restructure the code to remove the circular dependency at the module level. Common strategies include: \(1\) Moving the import statement inside the function or method that uses it \(lazy import\), so it executes at runtime after both modules are fully initialized; \(2\) Merging the two modules into one; \(3\) Extracting the shared dependency into a third module that both can import safely.
Journey Context:
You have \`models.py\` containing \`from .schemas import UserSchema\` and \`schemas.py\` containing \`from .models import User\`. When you start your application, you get \`AttributeError: partially initialized module 'models' has no attribute 'User' \(most likely due to a circular import\)\`. The traceback shows \`models.py\` line 1 importing schemas, then \`schemas.py\` line 1 importing models, then the error. You try reordering imports, putting them at the bottom of the file, but it still fails because the class definition in \`schemas.py\` requires \`User\` at class definition time, which happens during import. You realize that when \`schemas.py\` is imported halfway through \`models.py\` loading, \`models\` is in \`sys.modules\` but hasn't executed the \`class User\` line yet. The fix is to move \`from .models import User\` inside the method that uses it in \`schemas.py\`, or to create a \`types.py\` file that both \`models.py\` and \`schemas.py\` import from, breaking the cycle. After moving the import into the function body, the module loads successfully because the cross-reference is deferred until runtime when both modules are fully initialized.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:16:35.087433+00:00— report_created — created