uv
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseuv Package Manager
uv包管理器
Prefer uv over pip/poetry for Python dependency and project management. Use the workflow below and choose the right mode (project, script, or tool).
在Python依赖和项目管理中,优先选择uv而非pip/poetry。请遵循以下工作流,并选择合适的模式(项目、脚本或工具)。
Mode Decision
模式选择
User needs to...
│
├─ Repo uses poetry / pipenv / conda (poetry.lock, Pipfile, environment.yml)
│ → Ask: "This repo uses [poetry/pipenv/conda]. Do you want to switch to uv?"
│ → If yes: prefer the migrate-to-uv tool (uvx migrate-to-uv in project root) to convert metadata and lockfile; then uv sync. Remove legacy files only after user confirms.
│ → If no: do not use uv for project management; use the existing tool or uv pip only if appropriate.
│
├─ Repo has requirements.txt (no pyproject.toml)
│ → Ask: "This repo uses requirements.txt. Do you want to convert to uv (pyproject.toml + uv.lock)?"
│ → If yes: guide conversion (uv init, uv add from requirements, then uv sync)
│ → If no: use Pip-Compatible workflow (uv pip)
│
├─ Manage a project (pyproject.toml, lockfile, team repo)
│ → Use PROJECT workflow (uv init, uv add, uv sync, uv run)
│
├─ Run a single script with dependencies
│ → Use SCRIPT workflow (uv add --script, uv run <script>)
│
├─ Run a one-off CLI tool (no project)
│ → Use uvx: uvx <package> [args] or uv tool install <package>
│
└─ User declined conversion; existing pip/requirements workflow
→ Use uv pip (uv venv, uv pip sync, uv pip compile)User needs to...
│
├─ Repo uses poetry / pipenv / conda (poetry.lock, Pipfile, environment.yml)
│ → Ask: "This repo uses [poetry/pipenv/conda]. Do you want to switch to uv?"
│ → If yes: prefer the migrate-to-uv tool (uvx migrate-to-uv in project root) to convert metadata and lockfile; then uv sync. Remove legacy files only after user confirms.
│ → If no: do not use uv for project management; use the existing tool or uv pip only if appropriate.
│
├─ Repo has requirements.txt (no pyproject.toml)
│ → Ask: "This repo uses requirements.txt. Do you want to convert to uv (pyproject.toml + uv.lock)?"
│ → If yes: guide conversion (uv init, uv add from requirements, then uv sync)
│ → If no: use Pip-Compatible workflow (uv pip)
│
├─ Manage a project (pyproject.toml, lockfile, team repo)
│ → Use PROJECT workflow (uv init, uv add, uv sync, uv run)
│
├─ Run a single script with dependencies
│ → Use SCRIPT workflow (uv add --script, uv run <script>)
│
├─ Run a one-off CLI tool (no project)
│ → Use uvx: uvx <package> [args] or uv tool install <package>
│
└─ User declined conversion; existing pip/requirements workflow
→ Use uv pip (uv venv, uv pip sync, uv pip compile)Project Workflow
项目工作流
Use for apps, libraries, or any repo with .
pyproject.toml适用于应用程序、库或任何包含的仓库。
pyproject.tomlNew project
新项目
bash
uv init [project-name]
cd [project-name]
uv add <package> [package2...]
uv sync
uv run python main.pybash
uv init [project-name]
cd [project-name]
uv add <package> [package2...]
uv sync
uv run python main.pyor: uv run <any command>
or: uv run <any command>
undefinedundefinedExisting project (already has pyproject.toml)
现有项目(已包含pyproject.toml)
bash
uv sync # install from lockfile (or resolve and lock)
uv add <package> # add dependency, update lockfile and env
uv remove <package> # remove dependency
uv run <command> # run in project venv (creates .venv if needed)
uv lock # refresh lockfile onlybash
uv sync # install from lockfile (or resolve and lock)
uv add <package> # add dependency, update lockfile and env
uv remove <package> # remove dependency
uv run <command> # run in project venv (creates .venv if needed)
uv lock # refresh lockfile onlyPin Python version (optional)
固定Python版本(可选)
bash
uv python pin 3.11 # writes .python-version
uv python install 3.11 # ensure that version is availableRules:
- Use for project dependencies; avoid editing
uv addby hand for deps when uv can do it.pyproject.toml - After changing dependencies, run (or rely on
uv sync/uv addwhich update the env).uv remove - Run project commands via so the correct venv and env are used; do not assume
uv runor manual activate.pip install - When creating a new project, ensure is in
.venv(uv init usually adds it; add it if missing)..gitignore - Treat as a mandatory source-controlled artifact: commit it so all environments and CI use the same dependency versions; the lockfile is universal (one file for Windows, macOS, Linux).
uv.lock
bash
uv python pin 3.11 # writes .python-version
uv python install 3.11 # ensure that version is available规则:
- 项目依赖使用命令添加;当uv可以完成依赖管理时,避免手动编辑
uv add。pyproject.toml - 修改依赖后,运行(或依赖
uv sync/uv add命令,它们会自动更新环境)。uv remove - 通过运行项目命令,确保使用正确的虚拟环境和配置;不要假设用户已执行
uv run或手动激活虚拟环境。pip install - 创建新项目时,确保已添加到
.venv中(uv init通常会自动添加;如果缺失则手动添加)。.gitignore - 将视为必须纳入版本控制的文件:提交它以确保所有环境和CI使用相同的依赖版本;该锁文件是跨平台的(Windows、macOS、Linux通用)。
uv.lock
Script Workflow
脚本工作流
For a single Python file that needs packages (no full project).
- Add inline script metadata (or use to add deps to the file):
uv add --script
python
undefined适用于需要依赖包的单个Python文件(非完整项目)。
- 添加内联脚本元数据(或使用命令向文件添加依赖):
uv add --script
python
undefined/// script
/// script
requires-python = ">=3.10"
requires-python = ">=3.10"
dependencies = ["requests"]
dependencies = ["requests"]
///
///
import requests
print(requests.get("https://example.com"))
2. Add deps from CLI (updates the script file):
```bash
uv add --script example.py requests- Run with uv (creates an ephemeral env if needed):
bash
uv run example.pyFor executable scripts that run without typing , use a shebang (PEP 723):
uv runpython
#!/usr/bin/env -S uv run --scriptimport requests
print(requests.get("https://example.com"))
2. 通过CLI添加依赖(会更新脚本文件):
```bash
uv add --script example.py requests- 使用uv运行脚本(必要时会创建临时环境):
bash
uv run example.py对于无需输入即可直接运行的可执行脚本,使用shebang(符合PEP 723规范):
uv runpython
#!/usr/bin/env -S uv run --script/// script
/// script
requires-python = ">=3.10"
requires-python = ">=3.10"
dependencies = ["requests"]
dependencies = ["requests"]
///
///
**Rules:**
- Use `uv add --script <file>` to declare dependencies for that script.
- Use `uv run <script>.py` to run it; do not tell the user to `pip install` first.
- For portability, scripts can use shebang `#!/usr/bin/env -S uv run --script`.
**规则:**
- 使用`uv add --script <file>`为指定脚本声明依赖。
- 使用`uv run <script>.py`运行脚本;不要告知用户先执行`pip install`。
- 为了可移植性,脚本可使用shebang:`#!/usr/bin/env -S uv run --script`。Tools (one-off CLI from PyPI)
工具模式(来自PyPI的一次性CLI工具)
Run without installing globally:
bash
uvx <package> [args]无需全局安装即可运行:
bash
uvx <package> [args]e.g. uvx ruff check .
e.g. uvx ruff check .
e.g. uvx pycowsay "hello"
e.g. uvx pycowsay "hello"
Install the tool for repeated use:
```bash
uv tool install <package>
安装工具以便重复使用:
```bash
uv tool install <package>e.g. uv tool install ruff
e.g. uv tool install ruff
**Rules:**
- Prefer `uvx` for one-off runs; use `uv tool install` when the user will call the tool often.
**规则:**
- 一次性运行优先使用`uvx`;当用户需要频繁调用工具时,使用`uv tool install`。Repos Using requirements.txt
使用requirements.txt的仓库
When the repo has requirements.txt (and no ):
pyproject.toml- Ask the user: "This repo uses requirements.txt. Do you want to convert to uv (pyproject.toml + uv.lock)?"
- If yes: Guide conversion: , then
uv init(or, if you have requirements.in and locked requirements.txt,uv add -r requirements.txtto preserve versions), thenuv add -r requirements.in -c requirements.txt. Optionally remove or keep requirements.txt per user preference.uv sync - If no: Use the Pip-Compatible workflow below (no conversion).
Do not convert to uv without asking when the repo currently uses only requirements.txt.
当仓库仅包含requirements.txt(无)时:
pyproject.toml- 询问用户: "该仓库使用requirements.txt。是否要转换为uv(pyproject.toml + uv.lock)?"
- 如果用户同意: 引导转换:执行,然后
uv init(如果有requirements.in和已锁定的requirements.txt,使用uv add -r requirements.txt以保留版本),再执行uv add -r requirements.in -c requirements.txt。可根据用户偏好选择保留或删除requirements.txt。uv sync - 如果用户不同意: 使用以下兼容Pip的工作流(不转换)。
当仓库当前仅使用requirements.txt时,请勿未经询问就转换为uv。
Pip-Compatible Workflow
兼容Pip的工作流
For repos that still use or (and user did not want conversion):
requirements.txtpipbash
uv venv # create .venv
uv pip sync requirements.txt # install from locked requirements
uv pip compile requirements.in -o requirements.txt # compile/lockUse instead of for the same commands when you want speed and better resolution.
uv pippip适用于仍使用或的仓库(且用户不同意转换):
requirements.txtpipbash
uv venv # create .venv
uv pip sync requirements.txt # install from locked requirements
uv pip compile requirements.in -o requirements.txt # compile/lock当你追求速度和更优的依赖解析时,使用替代执行相同命令。
uv pippipCI and Docker
CI与Docker中的使用
When the user asks about uv in CI (e.g. GitHub Actions) or uv in Docker, advise using the patterns below and point to references/docker-and-ci.md for full detail.
CI (e.g. GitHub Actions):
- Install uv with the official action; use cache keyed by
astral-sh/setup-uvanduv.lock.pyproject.toml - Run so the job fails if the lockfile is out of sync (no silent deploy of untested deps).
uv sync --locked - Optionally run at end of job to keep cache lean.
uv cache prune --ci
Docker:
- Use a multi-stage build: builder stage with , copy only
ghcr.io/astral-sh/uvanduv.lockfirst, runpyproject.toml(or equivalent), then copyuv syncand app code into a slim runtime image (no uv/Rust in final image)..venv - Set and
UV_COMPILE_BYTECODE=1in the build stage; useUV_LINK_MODE=copywhen syncing so the final image doesn’t depend on source.--no-editable - Run the container as a non-root user when possible.
For step-by-step Dockerfiles and CI workflow examples, see references/docker-and-ci.md.
当用户询问CI中的uv(如GitHub Actions)或Docker中的uv时,建议使用以下模式,并指向references/docker-and-ci.md获取详细内容。
CI(如GitHub Actions):
- 使用官方Action安装uv;使用以
astral-sh/setup-uv和uv.lock为键的缓存。pyproject.toml - 运行,这样如果锁文件与配置不同步,任务会失败(避免静默部署未测试的依赖)。
uv sync --locked - 可选:在任务结束时运行以精简缓存。
uv cache prune --ci
Docker:
- 使用多阶段构建:构建阶段使用镜像,先仅复制
ghcr.io/astral-sh/uv和uv.lock,运行pyproject.toml(或等效命令),然后将uv sync和应用代码复制到轻量级运行时镜像(最终镜像中不含uv/Rust)。.venv - 在构建阶段设置和
UV_COMPILE_BYTECODE=1;同步时使用UV_LINK_MODE=copy参数,确保最终镜像不依赖源代码。--no-editable - 尽可能以非root用户运行容器。
如需分步Dockerfile和CI工作流示例,请查看references/docker-and-ci.md。
IDE Setup (Use uv .venv)
IDE配置(使用uv的.venv)
After uv creates or uses a project , automatically try to configure the IDE to use that interpreter so run/debug and IntelliSense use the same environment.
.venv- Target: Workspace settings (project-scoped, shareable):
.vscode/settings.json - Setting: pointing at the project’s
python.defaultInterpreterPath:.venv- Linux/macOS:
"${workspaceFolder}/.venv/bin/python" - Windows:
"${workspaceFolder}/.venv/Scripts/python.exe"
- Linux/macOS:
- Behavior: If exists, read it, add or update only
.vscode/settings.json, then write back. If it doesn’t exist, createpython.defaultInterpreterPathand the file with this setting. Preserve all other keys..vscode/ - When: After ,
uv init, or conversion from requirements.txt that creates/updatesuv sync..venv
Rules:
- Use workspace settings (), not user settings, so the choice is per-project and can be committed.
.vscode/settings.json - Do not overwrite or remove other settings in the file.
- If the workspace already has a Python interpreter selected, still add/update this key so the project’s is the default.
.venv
当uv创建或更新项目的后,自动尝试配置IDE使用该解释器,确保运行/调试和智能感知使用相同环境。
.venv- 目标: 工作区设置(项目级,可共享):
.vscode/settings.json - 设置项: 指向项目的
python.defaultInterpreterPath:.venv- Linux/macOS:
"${workspaceFolder}/.venv/bin/python" - Windows:
"${workspaceFolder}/.venv/Scripts/python.exe"
- Linux/macOS:
- 操作: 如果已存在,读取文件,仅添加或更新
.vscode/settings.json,然后写回。如果文件不存在,创建python.defaultInterpreterPath目录和该文件并添加此设置。保留所有其他配置项。.vscode/ - 时机: 在、
uv init或从requirements.txt转换后创建/更新uv sync时执行。.venv
规则:
- 使用工作区设置()而非用户设置,确保配置是项目级的且可提交。
.vscode/settings.json - 不要覆盖或删除文件中的其他设置。
- 如果工作区已选择Python解释器,仍需添加/更新此配置项,使项目的成为默认解释器。
.venv
Automation and Agent Behavior
自动化与Agent行为规范
- New Python project: Run (or
uv init), thenuv init <name>for initial deps, thenuv add. Suggestuv syncfor run/test commands.uv run - Add dependency: Use . For dev/optional:
uv add <package>.uv add --dev <package> - Run something: Use in a project;
uv run <command>for scripts;uv run script.pyfor one-off tools.uvx <tool> - No manual venv activate: Prefer so the agent and user don’t depend on
uv run.source .venv/bin/activate - Lockfile: Commit ; treat it as mandatory for parity. After pull or after editing deps, run
uv.lock. In CI, useuv syncso the job fails if the lockfile is out of sync withuv sync --locked.pyproject.toml - Detecting uv: If exists and there is no poetry/pipenv/conda (no poetry.lock, Pipfile, environment.yml), assume uv is allowed and suggest uv commands. If the user said "use uv", always prefer uv over pip/poetry. If poetry/pipenv/conda is present, ask before switching to uv (see Mode Decision).
pyproject.toml - requirements.txt only: If the repo has requirements.txt but no pyproject.toml, ask whether the user wants to convert to uv before converting or using uv pip.
- IDE interpreter: After uv creates or syncs , try to set the workspace Python interpreter by adding or updating
.venvinpython.defaultInterpreterPath(see IDE Setup above)..vscode/settings.json
- 新建Python项目: 运行(或
uv init),然后uv init <name>添加初始依赖,再执行uv add。建议使用uv sync执行运行/测试命令。uv run - 添加依赖: 使用。开发/可选依赖使用
uv add <package>。uv add --dev <package> - 运行任务: 项目中使用;脚本使用
uv run <command>;一次性工具使用uv run script.py。uvx <tool> - 无需手动激活虚拟环境: 优先使用,避免Agent和用户依赖
uv run命令。source .venv/bin/activate - 锁文件: 提交;将其视为确保环境一致性的必需文件。拉取代码或修改依赖后,运行
uv.lock。在CI中使用uv sync,这样如果锁文件与uv sync --locked不同步,任务会失败。pyproject.toml - 检测uv使用场景: 如果存在且无poetry/pipenv/conda(无poetry.lock、Pipfile、environment.yml),则假设允许使用uv并建议相关命令。如果用户明确要求"使用uv",则始终优先选择uv而非pip/poetry。如果存在poetry/pipenv/conda,切换到uv前需先询问用户(见模式选择部分)。
pyproject.toml - 仅使用requirements.txt的场景: 如果仓库有requirements.txt但无pyproject.toml,在转换或使用uv pip前需询问用户是否要转换为uv。
- IDE解释器配置: 当uv创建或同步后,尝试通过添加或更新
.venv中的.vscode/settings.json来设置工作区Python解释器(见上述IDE配置部分)。python.defaultInterpreterPath
Common Commands Reference
常用命令参考
| Task | Command |
|---|---|
| New project | |
| Add dependency | |
| Add dev dependency | |
| Remove dependency | |
| Install from lock | |
| Update lockfile | |
| Run in project | |
| Run script | |
| Run CLI tool once | |
| Install tool | |
| Create venv | |
| Pin Python | |
| Install Python | |
| 任务 | 命令 |
|---|---|
| 新项目创建 | |
| 添加依赖 | |
| 添加开发依赖 | |
| 删除依赖 | |
| 从锁文件安装依赖 | |
| 更新锁文件 | |
| 项目中运行命令 | |
| 运行脚本 | |
| 一次性运行CLI工具 | |
| 安装CLI工具 | |
| 创建虚拟环境 | |
| 固定Python版本 | |
| 安装指定Python版本 | |
Additional Resources
额外资源
- Full CLI and options: references/cli-and-commands.md
- Example flows: references/examples.md
- Common issues and fixes: references/troubleshooting.md
- Docker and CI: references/docker-and-ci.md
- Workspaces (monorepos) and resolution: references/workspaces-and-resolution.md
- 完整CLI及选项:references/cli-and-commands.md
- 示例流程:references/examples.md
- 常见问题与修复:references/troubleshooting.md
- Docker与CI:references/docker-and-ci.md
- 工作区(单体仓库)与依赖解析:references/workspaces-and-resolution.md