mirror of
https://github.com/fastapi/fastapi.git
synced 2026-07-01 18:46:45 -04:00
🌐 Update translations for zh (update-outdated) (#15177)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Yurii Motov <yurii.motov.monte@gmail.com>
This commit is contained in:
committed by
GitHub
parent
453988f359
commit
12bbd9453f
@@ -1,15 +1,15 @@
|
||||
# FastAPI CLI { #fastapi-cli }
|
||||
|
||||
**FastAPI CLI** 是一个命令行程序,你可以用它来部署和运行你的 FastAPI 应用程序,管理你的 FastAPI 项目,等等。
|
||||
**FastAPI <abbr title="command line interface - 命令行接口">CLI</abbr>** 是一个命令行程序,你可以用它来部署和运行你的 FastAPI 应用、管理 FastAPI 项目,等等。
|
||||
|
||||
当你安装 FastAPI 时(例如使用 `pip install "fastapi[standard]"`),会包含一个名为 `fastapi-cli` 的软件包,该软件包在终端中提供 `fastapi` 命令。
|
||||
当你安装 FastAPI(例如使用 `pip install "fastapi[standard]"`)时,会附带一个可以在终端中运行的命令行程序。
|
||||
|
||||
要在开发环境中运行你的 FastAPI 应用,你可以使用 `fastapi dev` 命令:
|
||||
要在开发环境中运行你的 FastAPI 应用,可以使用 `fastapi dev` 命令:
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid">main.py</u>
|
||||
$ <font color="#4E9A06">fastapi</font> dev
|
||||
|
||||
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
|
||||
|
||||
@@ -46,13 +46,66 @@ $ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid
|
||||
|
||||
</div>
|
||||
|
||||
该命令行程序 `fastapi` 就是 **FastAPI CLI**。
|
||||
/// tip | 提示
|
||||
|
||||
FastAPI CLI 接收你的 Python 程序路径(例如 `main.py`),自动检测 `FastAPI` 实例(通常命名为 `app`),确定正确的导入方式,然后启动服务。
|
||||
在生产环境中,你会使用 `fastapi run` 而不是 `fastapi dev`。🚀
|
||||
|
||||
在生产环境中,你应该使用 `fastapi run` 命令。🚀
|
||||
///
|
||||
|
||||
在内部,**FastAPI CLI** 使用了 <a href="https://www.uvicorn.dev" class="external-link" target="_blank">Uvicorn</a>,这是一个高性能、适用于生产环境的 ASGI 服务器。😎
|
||||
在内部,**FastAPI CLI** 使用 [Uvicorn](https://www.uvicorn.dev),这是一个高性能、适用于生产环境的 ASGI 服务器。😎
|
||||
|
||||
`fastapi` CLI 会尝试自动检测要运行的 FastAPI 应用,默认假设它是文件 `main.py` 中名为 `app` 的对象(或少数其他变体)。
|
||||
|
||||
但你也可以显式配置要使用的应用。
|
||||
|
||||
## 在 `pyproject.toml` 中配置应用的 `entrypoint` { #configure-the-app-entrypoint-in-pyproject-toml }
|
||||
|
||||
你可以在 `pyproject.toml` 文件中配置应用的位置,例如:
|
||||
|
||||
```toml
|
||||
[tool.fastapi]
|
||||
entrypoint = "main:app"
|
||||
```
|
||||
|
||||
这个 `entrypoint` 会告诉 `fastapi` 命令按如下方式导入应用:
|
||||
|
||||
```python
|
||||
from main import app
|
||||
```
|
||||
|
||||
如果你的代码结构如下:
|
||||
|
||||
```
|
||||
.
|
||||
├── backend
|
||||
│ ├── main.py
|
||||
│ ├── __init__.py
|
||||
```
|
||||
|
||||
那么你可以将 `entrypoint` 设置为:
|
||||
|
||||
```toml
|
||||
[tool.fastapi]
|
||||
entrypoint = "backend.main:app"
|
||||
```
|
||||
|
||||
这等价于:
|
||||
|
||||
```python
|
||||
from backend.main import app
|
||||
```
|
||||
|
||||
### 带路径的 `fastapi dev` { #fastapi-dev-with-path }
|
||||
|
||||
你也可以把文件路径传给 `fastapi dev` 命令,它会猜测要使用的 FastAPI 应用对象:
|
||||
|
||||
```console
|
||||
$ fastapi dev main.py
|
||||
```
|
||||
|
||||
但每次运行 `fastapi` 命令都需要记得传入正确的路径。
|
||||
|
||||
另外,其他工具可能找不到它,例如 [VS Code 扩展](editor-support.md) 或 [FastAPI Cloud](https://fastapicloud.com),因此推荐在 `pyproject.toml` 中使用 `entrypoint`。
|
||||
|
||||
## `fastapi dev` { #fastapi-dev }
|
||||
|
||||
@@ -70,6 +123,6 @@ FastAPI CLI 接收你的 Python 程序路径(例如 `main.py`),自动检
|
||||
|
||||
/// tip | 提示
|
||||
|
||||
你可以在[部署文档](deployment/index.md){.internal-link target=_blank}中了解更多。
|
||||
你可以在[部署文档](deployment/index.md)中了解更多。
|
||||
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user