ImportError: cannot import name '_request_ctx_stack' from 'flask'
Fixes `ImportError: cannot import name '_request_ctx_stack' from 'flask'` in flask. Use when upgrading to Flask 2.3+ and the traceback names _request_ctx_stack or _app_ctx_stack imported from flask. Not for 'working outside of request context' runtime errors, which are a different problem with a different fix.
ImportError: cannot import name 'requestctx_stack' from 'flask'
TL;DR
Flask removed the private requestctxstack in 2.3. Rewrite code that touches it to use the public proxies instead: flask.request, flask.g, and flask.currentapp cover nearly every use. Short term, pinning Flask below 2.3 also works.
The error
ImportError: cannot import name '_request_ctx_stack' from 'flask'Fix it
- Find usages: grep -rEn "requestctxstack|appctxstack" --include=*.py .
Expected: A small list of files touching the private stacks.
- Replace requestctxstack.top with the public equivalent. Most code wants flask.request, flask.session, or flask.g; app context access becomes flask.currentapp.
Expected: The module imports and request handling works as before.
- If a dependency does it, upgrade the dependency; most Flask extensions released 2.3-compatible versions.
Expected: pip show [package] lists a version from mid-2023 or later.
- Only as a stopgap: pip install 'flask<2.3'. Then schedule the rewrite, since the private API is gone for good.
Expected: App boots on the pinned version.
When to use this
Use when upgrading to Flask 2.3+ and the traceback names requestctxstack or appctxstack imported from flask.
When NOT to use this
Not for 'working outside of request context' runtime errors, which are a different problem with a different fix.
Version notes
Private context stacks removed in Flask 2.3. Public proxies (request, g, current_app, session) are stable across 2.x and 3.x.
Why it happens
Flask 2.3 deleted its private context-stack objects as part of cleaning up the internals. Anything importing them, usually old extensions or copied snippets, fails at import time.
Edge cases
flask.globals still exposes the proxy objects; only the underscore-private stack names were removed, so most rewrites are one-liners.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.