# 运行时错误：未设置密钥导致会话不可用

- **ID:** `python/flask-session-cookie-not-signed`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用会话的Flask应用必须通过`app.secret_key`设置密钥；未设置时，会话操作会抛出运行时错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (100% 成功率)
   ```
   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')`.
   ```
2. **** (95% 成功率)
   ```
   If using an app factory, set it in `create_app()` before returning the app.
   ```

## 无效尝试

- **** — Setting `app.config['SECRET_KEY']` after the first request doesn't help; it must be set before any session is used. (90% 失败率)
- **** — Using an empty string as secret key still raises the error because Flask checks for truthiness. (95% 失败率)
