python
runtime_error
ai_generated
true
RuntimeError: Working outside of request context.
ID: python/flask-request-context-missing
80%Fix Rate
87%Confidence
0Evidence
2024-04-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
| 3.x | active | — | — | — |
Root Cause
Accessing request, session, or g outside an active HTTP request (e.g., in a scheduled job or test without test_request_context).
generic中文
在活跃 HTTP 请求之外访问 request、session 或 g(例如在定时任务或未使用 test_request_context 的测试中)。
Workarounds
-
95% success
Capture needed values inside the view: `data = request.get_json()` then pass `data` to the background function.
-
90% success
In tests: `with app.test_request_context('/path', json={...}):` before calling code that reads request.
Dead Ends
Common approaches that don't work:
-
90% fail
request is a LocalProxy; module-level access still raises the same error.
-
85% fail
request.form/args access still throws; caller gets None or wrong data.