python module_error ai_generated true

ImportError: cannot import name 'bp' from partially initialized module 'app.routes' (most likely due to a circular import)

ID: python/flask-circular-import-blueprint

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-24First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —
3.x active — — —

Root Cause

Two modules import each other, often app -> routes -> app for `app` object or blueprint registration.

generic

中文

两个模块相互导入,通常是app -> routes -> app以获取app对象或注册蓝图。

Workarounds

  1. 93% success
    def create_app():\n    app = Flask(__name__)\n    from .routes import bp\n    app.register_blueprint(bp)\n    return app
  2. 88% success
    def register():\n    from .routes import bp\n    app.register_blueprint(bp)

Dead Ends

Common approaches that don't work:

  1. 65% fail

    Still executed at import time; cycle remains.

  2. 90% fail

    Cycle is structural, not naming.