python config_error ai_generated true

RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

ID: python/flask-secret-key-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-01-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Flask's session cookie signing requires app.secret_key, but it was never configured before using session or flash.

generic

中文

Flask 的会话 Cookie 签名需要 app.secret_key,但在使用 session 或 flash 之前未配置。

Workarounds

  1. 95% success
    app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
  2. 90% success
    app.secret_key = secrets.token_hex(32) for dev only

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Flask treats empty string as falsy and raises the same error.

  2. 85% fail

    No such option; sessions still require a key.