# 运行时错误：由于未设置密钥，会话不可用。

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

## 根因

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

## 版本兼容性

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

## 解决方案

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

## 无效尝试

- **** — Empty key is considered unset and raises same error. (95% 失败率)
- **** — Sessions invalidate on restart; need consistent key. (70% 失败率)
