# RuntimeError: 会话不可用，因为未设置密钥。请为应用设置唯一且保密的 secret_key。

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

## 根因

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

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Set a secret key from environment:
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
   ```
2. **** (85% 成功率)
   ```
   For tests only, generate a fixed key:
app.config['SECRET_KEY'] = 'test-key'
   ```

## 无效尝试

- **** — Permanence affects cookie lifetime, not the required signing key. (90% 失败率)
- **** — Most session backends still need a secret key for signing. (70% 失败率)
