You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/posts/2025/2025-08-26-python-uv-cheat-sheet.md
+69-29Lines changed: 69 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,22 +7,29 @@ categories:
7
7
comments: true
8
8
date:
9
9
created: 2025-08-26
10
+
updated: 2025-08-28
10
11
---
11
12
12
13
# Python uv cheat sheet
13
14
14
-
Python [UV](https://docs.astral.sh/uv/) common usage cheat sheet, but doesn't cover all the features.
15
+
Python [uv](https://docs.astral.sh/uv/) common usage cheat sheet, but doesn't cover all the features.
15
16
16
17
<!-- more -->
17
18
18
19
## Init project
19
20
20
-
```bash
21
-
# init new project with new folder
22
-
uv init my-project -p python3.13
21
+
uv init can have 3 templates: [`--app`](https://docs.astral.sh/uv/concepts/projects/init/#packaged-applications) (by default), [`--lib`](https://docs.astral.sh/uv/concepts/projects/init/#applications) and [`--package`](https://docs.astral.sh/uv/concepts/projects/init/#packaged-applications).
|`uv init my-project -p python3.13`| Init new project with new folder |
26
+
|`uv init -p python3.13`| Init an existing project with python3.13 |
27
+
|`uv init --lib`| Libraries template creates a src folder whereas application template only creates a main.py file |
28
+
|`uv init --package`| Init a project with package template, same as libraries but pyproject.toml has a `[project.scripts]` key, so this is for command-line interface you can later run the command by: `uv run pkg-1`|
23
29
24
-
# init an existing project
25
-
uv init -p python3.13
30
+
```toml title="pyproject.toml created by uv init --package pkg-1"
31
+
[project.scripts]
32
+
pkg-1 = "pkg_1:main"
26
33
```
27
34
28
35
!!! warning "uv init under a folder with already a pyproject.toml"
@@ -100,6 +107,27 @@ typing = [
100
107
101
108
```
102
109
110
+
### Add to dependency sources
111
+
112
+
[Dependency sources](https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-sources) are for local development only.
|`uv sync --no-dev`| Install [dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/#project-dependencies) only (without any extra nor any group) |
175
+
|`uv sync`| Install dependencies and `dev` group, no extras, no other groups than `dev`|
176
+
|`uv sync --all-groups`| Install dependencies and all groups ([`dependency-groups`](https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-groups)) |
177
+
|`uv sync --all-extras`| Install dependencies and all extras ([`project.optional-dependencies`](https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies)) and `dev` group (`dev` group is by default) |
178
+
|`uv sync --all-extras --all-groups`| Install dependencies and all extras and all groups |
179
+
|`uv sync --extra aio`| Install dependencies and extra `aio` and `dev` group |
180
+
|`uv sync --extra aio --no-dev`| Install dependencies and extra `aio` but without `dev` group |
181
+
|`uv sync --extra aio --inexact`| Install dependencies and `dev` groups and retain already installed [extraneous packages](https://docs.astral.sh/uv/concepts/projects/sync/#retaining-extraneous-packages) not declared in pyproject.toml |
182
+
|`uv sync --locked --no-dev`| Ensure install by respecting `uv.lock` (ensure uv.lock won't be changed after uv sync) and raise error if lock file doesn't conform with pyproject.toml.<br/><br/>In 🐳 Dockerfile ([official uv Dockerfile example](https://github.com/astral-sh/uv-docker-example/blob/main/Dockerfile)), we often use `uv sync --locked --no-install-project --no-dev`, see [Using uv in Docker](https://docs.astral.sh/uv/guides/integration/docker/) to understand the usage of each parameters. |
152
183
153
-
- Ensure install by respecting `uv.lock` (ensure uv.lock won't be changed after uv sync) and raise error if lock file doesn't conform with pyproject.toml: `uv sync --locked --no-dev`, in Dockerfile ([official uv Dockerfile example](https://github.com/astral-sh/uv-docker-example/blob/main/Dockerfile)), we often use `uv sync --locked --no-install-project --no-dev`, see [Using uv in Docker](https://docs.astral.sh/uv/guides/integration/docker/) to understand the usage of each parameters.
154
-
155
-
```bash
156
-
$ uv sync --locked --no-dev
157
-
Resolved 21 packages in 33ms
158
-
The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`.
159
-
```
184
+
```bash
185
+
$ uv sync --locked --no-dev
186
+
Resolved 21 packages in 33ms
187
+
The lockfile at `uv.lock` needs to be updated, but `--locked` was provided.
188
+
To update the lockfile, run `uv lock`.
189
+
```
160
190
161
191
## Dependencies tree
162
192
163
-
- `uv pip tree`: display the **installed packages**in a tree format.
164
-
- `uv tree`: update `uv.lock` based on `pyproject.toml` and display tree based on `uv.lock`, **no package installation will occur**. `uv tree` displays better than `uv tree pip tree`
165
-
- `uv tree --frozen`: don't update `uv.lock`, just display tree based on the current `uv.lock`
166
-
- `uv tree --locked`: if `uv.lock` is not updated, display a warning message. This command is not very useful.
|`uv pip tree`| Display the **installed packages** in a tree format |
196
+
|`uv tree`| Update `uv.lock` based on `pyproject.toml` and display tree based on `uv.lock`, **no package installation will occur**. `uv tree` displays better than `uv pip tree`|
197
+
|`uv tree --frozen`| Don't update `uv.lock`, just display tree based on the current `uv.lock`|
198
+
|`uv tree --locked`| If `uv.lock` is not updated, display a warning message. This command is not very useful |
167
199
168
200
## List outdated packages
169
201
170
202
-`uv tree --outdated`: display a list of outdated packages with their latest versions.
171
203
172
204
## Upgrade packages and uv.lock
173
205
174
-
- `uv lock`: update the `uv.lock` file to match the current state of `pyproject.toml`.
175
-
- `uv lock -U`: update the `uv.lock` file and upgrade all packages to their latest compatible versions.
176
-
- `uv sync`: same as `uv lock` but also installs the dependencies.
177
-
- `uv sync -U`: same as `uv lock -U` but also installs the dependencies.
206
+
[`uv.lock` file](https://docs.astral.sh/uv/concepts/projects/layout/#the-lockfile)can be updated by `uv lock`, `uv sync`, `uv run`, `uv add`, `uv remove`.
|`uv lock`| Update the `uv.lock` file to match the current state of `pyproject.toml`|
211
+
|`uv lock -U`| Update the `uv.lock` file and [upgrade](https://docs.astral.sh/uv/concepts/projects/sync/#upgrading-locked-package-versions) all packages to their latest compatible versions |
212
+
|`uv sync`| Same as `uv lock` but also installs the dependencies |
213
+
|`uv sync -U`| Same as `uv lock -U` but also installs the dependencies |
178
214
179
215
!!! note "`uv lock` vs `uv lock -U` and `uv sync` vs `uv sync -U`"
180
216
If latest version of fastapi is 0.116.1, and pyproject.toml declares fastapi>=0.115.1, and current uv.lock has fastapi==0.115.1. then:
@@ -187,3 +223,7 @@ all = [
187
223
## Integrations
188
224
189
225
Check [this doc](https://docs.astral.sh/uv/guides/integration/) for more information on integrations with other tools and platforms (Docker, Jupyter, Github Actions, Pre Commit, PyTorch FastAPI, etc.).
226
+
227
+
## Build
228
+
229
+
uv can also [build with extension module by `--build-backend` flag](https://docs.astral.sh/uv/concepts/projects/init/#projects-with-extension-modules) to work with Rust and C, C++, CPython etc.
0 commit comments