# RuntimeError: Working outside of application context.

- **ID:** `python/flask-app-context-runtime-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to access Flask's application context (e.g., current_app, g, url_for) outside of a request or app context, typically in a background thread or standalone script.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   with app.app_context():\n    # access current_app or url_for\n    pass
   ```
2. **** (90% success)
   ```
   Within a Flask request handler, the context is automatically available; move the code inside a view function.
   ```

## Dead Ends

- **** — Using app.test_request_context() without entering it works only within a with block, but calling it outside causes the same error. (70% fail)
- **** — Setting app.app_context().push() manually can leak context and cause issues in multi-threaded environments. (60% fail)
