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-required

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Flask session (or CSRF) requires app.secret_key but it was never configured, often in tests or a fresh deploy.

generic

中文

Flask 会话(或 CSRF)需要 app.secret_key,但从未配置,常见于测试或新部署环境。

Workarounds

  1. 95% success
    Set a secret key from environment:
    app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
  2. 85% success
    For tests only, generate a fixed key:
    app.config['SECRET_KEY'] = 'test-key'

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Permanence affects cookie lifetime, not the required signing key.

  2. 70% fail

    Most session backends still need a secret key for signing.