python
config_error
ai_generated
true
RuntimeError: The session is unavailable because no secret key was set. Please set the secret key on the application to something unique and secret.
ID: python/flask-session-cookie-not-signed
80%Fix Rate
90%Confidence
0Evidence
2024-02-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Flask applications using sessions must set a secret key via `app.secret_key`; without it, session operations raise a RuntimeError.
generic中文
使用会话的Flask应用必须通过`app.secret_key`设置密钥;未设置时,会话操作会抛出运行时错误。
Workarounds
-
100% success
Set `app.secret_key = 'your-secret-key'` in your app creation code, preferably from environment variables: `import os; app.secret_key = os.environ.get('SECRET_KEY', 'dev-key')`. -
95% success
If using an app factory, set it in `create_app()` before returning the app.
Dead Ends
Common approaches that don't work:
-
90% fail
Setting `app.config['SECRET_KEY']` after the first request doesn't help; it must be set before any session is used.
-
95% fail
Using an empty string as secret key still raises the error because Flask checks for truthiness.