# 运行时错误：在应用程序上下文之外工作。

- **ID:** `python/flask-app-context-import-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在请求或应用程序上下文之外尝试访问 Flask 功能，如 current_app 或 url_for。

## 版本兼容性

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

## 解决方案

1. **** (100% 成功率)
   ```
   Use with app.app_context(): before accessing current_app or url_for
   ```
2. **** (95% 成功率)
   ```
   If in a CLI command, use app = create_app() and then with app.app_context():
   ```

## 无效尝试

- **** — You need to push an app context manually. (80% 失败率)
- **** — current_app is a proxy that requires an active context. (100% 失败率)
