python runtime_error ai_generated true

运行时错误:未找到应用程序。要么您不在 Starlette 应用程序中运行,要么未设置 'app' 变量。

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

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2024-11-18首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 80% 失败

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