python config_error ai_generated true

RuntimeError: The session is unavailable because no secret key was set.

ID: python/flask-session-cookie-secret-key

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Flask session requires a secret key for signing; not set in app.config.

generic

中文

Flask 会话需要用于签名的密钥;未在 app.config 中设置。

Workarounds

  1. 95% success
    app.secret_key = 'your-secret-key' # or app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
  2. 90% success
    Generate secure key: import secrets
    app.secret_key = secrets.token_hex(16)

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Empty key is considered unset and raises same error.

  2. 70% fail

    Sessions invalidate on restart; need consistent key.