ImportError: cannot import name 'User' from partially initialized module 'models' (most likely due to a circular
Fixes ImportError: cannot import name 'User' from partially initialized module 'models' (most likely due to a circular import) Use when the error says 'partially initialized module' and 'most likely due to a circular import' Not for: the module truly lacks the name (typo) - then it is a plain ImportError
How to fix ImportError: cannot import name 'User' from partially initialized module 'models' (most likely due to a circular import)
Two modules import each other at the top level, so one of them is not finished when the other asks for a name. Break the cycle: move the shared name into a third module both import, or move one import inside the function that uses it, or import models and use models.User (attribute lookup happens at call time, after init). Restructuring beats the local-import band-aid.
The exact error
ImportError: cannot import name 'User' from partially initialized module 'models' (most likely due to a circular import)Fix it
- Map the cycle: read the traceback's import chain (a imports b imports a).
Expected: you see the loop
- Best: extract the shared class/function into a third module both import.
Expected: no cycle, imports clean
- Quick: move one import inside the function using it, or switch to
import models+models.User.
Expected: works; revisit the structure later
When this applies
the error says 'partially initialized module' and 'most likely due to a circular import'
When this does NOT apply
the module truly lacks the name (typo) - then it is a plain ImportError
Versions
any Python 3
Why it happens
Python executes module top-level code on first import; a cycle means someone reads names from a half-executed module.
Edge cases
TYPE_CHECKING imports dodge runtime cycles for annotations; import x plus attribute access is the idiomatic runtime dodge.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.