python
module_error
ai_generated
true
ImportError: cannot import name 'db' from partially initialized module 'app' (most likely due to a circular import)
ID: python/flask-circular-import
80%Fix Rate
88%Confidence
0Evidence
2024-02-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Two modules import each other at module load time, typically models importing app and app importing models.
generic中文
两个模块在加载时互相导入,通常是 models 导入 app 而 app 又导入 models。
Workarounds
-
93% success
def create_app(): app = Flask(__name__) db.init_app(app) return app -
91% success
# extensions.py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy()
Dead Ends
Common approaches that don't work:
-
85% fail
Only changes which side fails first; cycle remains.
-
90% fail
Leaves db undefined and crashes later.