uv
是一个用 Rust 开发的超高性能 Python 包管理器。pip
工作流的即插即用替代方案。(译注:uv 在 2024.02 发布,本文写于 2024.08,翻译于 2025.05)pip
替代品扩展成为一个端到端的解决方案,可用于管理 Python 项目、命令行工具、单文件脚本,甚至 Python 本身。Cargo
:提供了一个快速、可靠且易用的统一接口。pip
工作流的即插即用替代方案。uv run
、uv lock
和 uv sync
。uv 现在能基于标准元数据创建跨平台的锁文件,并利用该文件来安装依赖。它是 Poetry、PDM 和 Rye 等工具的高性能替代品。uv tool install
和 uv tool run
(别名为 uvx
)。uv 能在隔离的虚拟环境中安装命令行工具,还能无需先安装就直接执行命令(如 uvx ruff check
)。它是 pipx
等工具的高性能替代品。uv python install
。uv 现在可以直接帮你安装 Python,替代 pyenv
等工具。uv run
命令就能执行这些独立的 Python 脚本。uv pip
(我们将它作为一等功能,继续维护并改进),uv 适用于任何 Python 工作流,从一次性脚本到大型的多包工作区开发。curl -LsSf https://astral.sh/uv/install.sh | sh
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
pip install uv
pipx install uv
[project]
name = "hello-world"
version = "0.1.0"
readme = "README.md"
dependencies = ["fastapi>=0.112"]
[[package]]
name = "fastapi"
version = "0.112.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "pydantic" },
{ name = "starlette" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/2c/09/71a961740a1121d7cc90c99036cc3fbb507bf0c69860d08d4388f842196b/fastapi-0.112.1.tar.gz", hash = "sha256:b2537146f8c23389a7faa8b03d0bd38d4986e6983874557d95eed2acc46448ef", size = 291025 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/39/b0/0981f9eb5884245ed6678af234f2cbcd40f44570718caddc0360bdb4015d/fastapi-0.112.1-py3-none-any.whl", hash = "sha256:bcbd45817fc2a1cd5da09af66815b84ec0d3d634eb173d1ab468ae3103e183e4", size = 93163 },
]
[[package]]
name = "fastapi-cli"
version = "0.0.5"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typer" },
{ name = "uvicorn", extra = ["standard"] },
]
sdist = { url = "https://files.pythonhosted.org/packages/c5/f8/1ad5ce32d029aeb9117e9a5a9b3e314a8477525d60c12a9b7730a3c186ec/fastapi_cli-0.0.5.tar.gz", hash = "sha256:d30e1239c6f46fcb95e606f02cdda59a1e2fa778a54b64686b3ff27f6211ff9f", size = 15571 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/24/ea/4b5011012ac925fe2f83b19d0e09cee9d324141ec7bf5e78bb2817f96513/fastapi_cli-0.0.5-py3-none-any.whl", hash = "sha256:e94d847524648c748a5350673546bbf9bcaeb086b33c24f2e82e021436866a46", size = 9489 },
]
uv lock
,uv 仍然会为 Linux 和 Windows 生成解析方案,即使每个平台上所需的依赖集稍有不同。uv run
命令,它能在项目专属环境中运行命令,而且不需要手动激活虚拟环境。uv run
非常快速。每次执行时,它都会自动重新锁定和同步项目,确保你的环境始终是最新状态。完全不需要手动干预。uv run
能确保你的命令每次都在一个一致的、受锁文件管理的环境中运行。uv run
命令启动 FastAPI 应用的示例。uv run
,你再也不需要操心虚拟环境的激活、依赖包的管理或者项目的维护更新。一切都变得无比简单。本文翻译并首发于 Python猫:uv:统一的 Python 包管理
project.dependencies
部分,你可以定义项目的依赖和可发布的元数据。而在开发过程中,uv 还允许你通过 tool.uv.sources
为这些依赖项指定替代来源。anyio
,你可以运行 uv add --editable ../anyio
来生成以下 pyproject.toml
:[project]
name = "hello-world"
version = "0.1.0"
readme = "README.md"
dependencies = ["anyio"]
[tool.uv.sources]
anyio = { path = "../anyio", editable = true }
anyio
的依赖。但在本地开发时,使用 uv run
可以将 ../anyio
路径下的可编辑的包加到开发环境中。这样,项目的依赖定义保持不变,只是依赖的来源发生了变化。pyproject.toml
,但工作区共享一个锁文件,确保工作区使用一致的依赖集运行。[project]
name = "fastapi"
version = "0.1.0"
readme = "README.md"
dependencies = ["uvicorn"]
[tool.uv.sources]
uvicorn = { workspace = true }
[tool.uv.workspace]
members = ["libraries/*"]
uv run --package fastapi
或 uv run --package uvicorn
。uv tool install
在专用的隔离虚拟环境中安装命令行工具(如 Ruff)uvx
直接运行一次性命令,无需预先安装uvx posting
就能直接运行 Darren Burns 开发的 posting
终端界面程序(TUI)。uvx
命令在终端中运行 posting 的示例。uv tool list
查看所有已安装的工具及其可执行文件uv tool upgrade --all
将所有工具升级到最新版本uvx
执行命令几乎没有任何额外开销。$ curl -LsSf https://astral.sh/uv/install.sh | sh
$ uv python install 3.12
uv python
外,uv 还可以根据需要,自动下载所需的 Python 版本(如 uv run
、uv venv
等命令)。$ apt-get update && apt-get install -y curl
$ curl -LsSf https://astral.sh/uv/install.sh | sh
$ source $HOME/.cargo/env
$ uvx posting
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
requests
和 rich
,但不包含任何显式的依赖元数据。早些时候,你需要单独管理这些依赖项,例如使用专门的 requirements.txt 文件。uv add
自动将依赖声明嵌入到脚本中:$ uv add --script main.py "requests<3" "rich"
$ cat main.py
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
uv run main.py
将在一个隔离的、临时的虚拟环境中执行脚本,并安装所有依赖项。--with
标记在运行时指定依赖项,例如:uv run --with "requests<3" --with rich main.py
。