-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a guide for using uv with FastAPI #6401
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Using uv with FastAPI | ||
|
||
[FastAPI](https://github.com/fastapi/fastapi) is modern, high-performance Python web framework. You | ||
can use uv to manage your FastAPI project, including installing dependencies, managing environments, | ||
running FastAPI applications, and more. | ||
|
||
!!! note | ||
|
||
You can view the source code for this guide in the [uv-fastapi-example](https://github.com/astral-sh/uv-fastapi-example) repository. | ||
|
||
As an example, consider the sample application defined in the | ||
[FastAPI documentation](https://fastapi.tiangolo.com/tutorial/bigger-applications/), structured as | ||
follows: | ||
|
||
```plaintext | ||
. | ||
├── app | ||
│ ├── __init__.py | ||
│ ├── main.py | ||
│ ├── dependencies.py | ||
│ └── routers | ||
│ │ ├── __init__.py | ||
│ │ ├── items.py | ||
│ │ └── users.py | ||
│ └── internal | ||
│ ├── __init__.py | ||
│ └── admin.py | ||
``` | ||
|
||
To migrate this project to uv, add a `pyproject.toml` file to the root directory of the project, as | ||
a sibling to the `app` directory. | ||
|
||
```plaintext | ||
. | ||
├── pyproject.toml | ||
├── app | ||
│ ├── __init__.py | ||
│ ├── main.py | ||
│ ├── dependencies.py | ||
│ └── routers | ||
│ │ ├── __init__.py | ||
│ │ ├── items.py | ||
│ │ └── users.py | ||
│ └── internal | ||
│ ├── __init__.py | ||
│ └── admin.py | ||
``` | ||
|
||
The contents of the `pyproject.toml` file should look something like this: | ||
|
||
```toml title="pyproject.toml" | ||
[project] | ||
name = "app" | ||
version = "0.1.0" | ||
description = "FastAPI project" | ||
readme = "README.md" | ||
requires-python = ">=3.12" | ||
dependencies = [ | ||
"fastapi[standard]", | ||
] | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
``` | ||
|
||
From there, you can run the FastAPI application with: | ||
|
||
```console | ||
$ uv run fastapi dev app/main.py | ||
``` | ||
|
||
`uv run` will automatically resolve and lock the project dependencies (i.e., create a `uv.lock` | ||
alongside the `pyproject.toml`), create a virtual environment, and run the command in that | ||
environment. | ||
|
||
## Initializing a FastAPI project | ||
|
||
We could reach a similar result to the above by creating a project from scratch with `uv init` and | ||
installing FastAPI with `uv add fastapi`, as in: | ||
|
||
```console | ||
$ uv init app | ||
$ cd app | ||
$ uv add fastapi --extra standard | ||
``` | ||
|
||
By default, uv uses a `src` layout for newly-created projects, so the `app` directory will be nested | ||
within a `src` directory. If you copied over the source code from the FastAPI tutorial, the project | ||
structure would look like this: | ||
|
||
```plaintext | ||
. | ||
├── pyproject.toml | ||
└── src | ||
└── app | ||
├── __init__.py | ||
├── main.py | ||
├── dependencies.py | ||
└── routers | ||
│ ├── __init__.py | ||
│ ├── items.py | ||
│ └── users.py | ||
└── internal | ||
├── __init__.py | ||
└── admin.py | ||
``` | ||
|
||
In this case, you would run the FastAPI application with: | ||
|
||
```console | ||
$ uv run fastapi dev src/app/main.py | ||
``` | ||
|
||
## Deployment | ||
|
||
To deploy the FastAPI application with Docker, you can use the following `Dockerfile`: | ||
|
||
```dockerfile title="Dockerfile" | ||
FROM python:3.12-slim | ||
|
||
# Install uv. | ||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv | ||
|
||
# Copy the application into the container. | ||
COPY . /app | ||
|
||
# Install the application dependencies. | ||
WORKDIR /app | ||
RUN uv sync --frozen --no-cache | ||
|
||
# Run the application. | ||
CMD ["/app/.venv/bin/fastapi", "run", "app/main.py", "--port", "80"] | ||
``` | ||
|
||
For more on using uv with Docker, see the [Docker guide](./docker.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we'd do "Building a REST API" and "Building a CLI" top-level guides — maybe after "Working on projects" and probably both just using whatever we feel like is good practice e.g. FastAPI and Typer(?). I guess they could go in the integration guides instead, but it seems nice to frame it around the general idea rather than the specific framework (even if we use a specific framework in the example). What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(will review the rest tomorrow)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer having something targeted like this. We could always expand it later if needed.