python runtime_error ai_generated true

RuntimeError: No application found. Either you are not running inside a Starlette application or you have not set the 'app' variable.

ID: python/starlette-runtime-error-no-application

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-11-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

试图在 Starlette 应用上下文之外使用 request 或 response 对象。

generic

中文

试图在 Starlette 应用上下文之外使用 request 或 response 对象。

Workarounds

  1. 100% success
    from starlette.requests import Request
    @app.route('/')
    async def index(request: Request):
        return JSONResponse({'message': 'Hello'})

Dead Ends

Common approaches that don't work:

  1. 95% fail

    request 对象依赖于请求上下文,无法在全局创建

  2. 80% fail

    没有正确初始化上下文,仍然会报错