Report #67826
[bug\_fix] AttributeError: partially initialized module 'models' has no attribute 'User' \(most likely due to a circular import\)
Refactor to remove the top-level circular dependency by moving the import statement inside the function or method where it is used \(lazy/local import\), or extract the shared components into a third module imported by both. For example, in \`views.py\`, change \`from .models import User\` to import inside the route function.
Journey Context:
Developer has \`models.py\` with \`class User\(Base\): ...\` and at the top of the file adds \`from .views import render\_user\` to use it in a method. In \`views.py\`, they have \`from .models import User\` to query the database. When the application starts, Python begins importing \`models\`. It sees the import of \`views\`, starts importing \`views\`, which immediately tries to import \`models\`. Since \`models\` is already in \`sys.modules\` but hasn't finished executing \(it's partially initialized\), Python returns the partial module object. \`views\` then tries to access \`models.User\`, but \`User\` hasn't been defined yet because \`models.py\` hasn't reached that line. This raises \`AttributeError: partially initialized module 'models' has no attribute 'User'\`. The developer tries moving the import to the bottom of \`models.py\`, which sometimes works by accident but is fragile. The correct fix is to break the cycle: either \`views\` imports \`models\` at runtime inside the function that needs it, or both import a common \`types.py\` that defines base classes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:19:26.155034+00:00— report_created — created