FastAPI 教程翻译 - 用户指南 35 - 调试

FastAPI 教程翻译 - 用户指南 35 - 调试

FastAPI Tutorial - User Guide - Debugging

You can connect the debugger in your editor, for example with Visual Studio Code or PyCharm.

您可以在编辑器中连接调试器,例如使用 Visual Studio Code 或 PyCharm。

Call uvicorn

调用 uvicorn

In your FastAPI application, import and run uvicorn directly:

在您的 FastAPI 应用程序中,直接导入并运行 uvicorn

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    a = "a"
    b = "b" + a
    return {"hello world": b}


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

About __name__ == "__main__"

关于 __name__ == "__main__"

The main purpose of the __name__ == "__main__" is to have some code that is executed when your file is called with:

__name__ == "__main__" 的主要目的是使一些使用以下代码调用文件时执行的代码:

python myapp.py

but is not called when another file imports it, like in:

但在另一个文件导入时不会被调用,例如:

from myapp import app
More details
更多细节

Let’s say your file is named myapp.py.

假设您的文件名为 myapp.py

If you run it with:

如果使用以下命令运行它:

python myapp.py

then the internal variable __name__ in your file, created automatically by Python, will have as value the string "__main__".

那么文件中的内部变量 __name__(由 Python 自动创建)的值将为字符串 "__main__"

So, the section:

因此,该部分:

    uvicorn.run(app, host="0.0.0.0", port=8000)

will run.

将会运行。


This won’t happen if you import that module (file).

如果导入该模块(文件),则不会发生这种情况。

So, if you have another file importer.py with:

因此,如果您还有另一个文件 importer.py,其中包含:

from myapp import app

# Some more code

in that case, the automatic variable inside of myapp.py will not have the variable __name__ with a value of "__main__".

在这种情况下,myapp.py 内部的自动变量将没有值为 "__main__" 的变量 __name__

So, the line:

因此,这一行:

    uvicorn.run(app, host="0.0.0.0", port=8000)

will not be executed.

将不会执行。

Info

信息

For more information, check the official Python docs.

有关更多信息,请查看 Python 官方文档

Run your code with your debugger

使用调试器运行代码

Because you are running the Uvicorn server directly from your code, you can call your Python program (your FastAPI application) directly from the debugger.

因为您是直接从代码运行 Uvicorn 服务器,所以可以直接从调试器调用 Python 程序(FastAPI应用程序)。


For example, in Visual Studio Code, you can:

例如,在 Visual Studio Code 中,您可以:

  • Go to the “Debug” panel.

    转到『调试』面板。

  • “Add configuration…”.

    『添加配置……』。

  • Select “Python”

    选择『Python』

  • Run the debugger with the option “Python: Current File (Integrated Terminal)”.

    使用选项『Python: Current File (Integrated Terminal)』运行调试器。

It will then start the server with your FastAPI code, stop at your breakpoints, etc.

然后,它将使用您的 FastAPI 代码启动服务器,在断点处停止,依此类推。

Here’s how it might look:

看起来像是这样:

tart the server with your FastAPI code, stop at your breakpoints, etc.

然后,它将使用您的 FastAPI 代码启动服务器,在断点处停止,依此类推。

Here’s how it might look:

看起来像是这样:

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值