astral-sh / uv
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
astral-sh / uv
uv An extremely fast Python package and project manager, written in Rust. Installing Trio’s dependencies with a warm cache. Highlights A single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more. 10-100x faster than pip. Provides comprehensive project management, with a universal lockfile. Runs scripts, with support for inline dependency metadata. Installs and manages Python versions. Runs and installs tools published as Python packages. Includes a pip-compatible interface for a performance boost with a familiar CLI. Supports Cargo-style workspaces for scalable projects. Disk-space efficient, with a global cache for dependency deduplication. Installable without Rust or Python via curl or pip. Supports macOS, Linux, and Windows. uv is backed by Astral, the creators of Ruff and ty.
uv 是一个用 Rust 编写的极速 Python 包和项目管理器。它可以在缓存预热的情况下安装 Trio 的依赖项。其亮点包括:作为一个单一工具,可替代 pip、pip-tools、pipx、poetry、pyenv、twine、virtualenv 等;速度比 pip 快 10-100 倍;提供全面的项目管理功能,并配有通用锁文件;支持运行带有内联依赖元数据的脚本;能够安装和管理 Python 版本;可以运行和安装作为 Python 包发布的工具;包含一个与 pip 兼容的接口,在提供熟悉 CLI 的同时提升性能;支持 Cargo 风格的工作区以实现可扩展项目;磁盘空间利用率高,通过全局缓存实现依赖去重;无需 Rust 或 Python 环境即可通过 curl 或 pip 安装;支持 macOS、Linux 和 Windows。uv 由 Ruff 和 ty 的创建者 Astral 提供支持。
Installation Install uv with our standalone installers: # On macOS and Linux. curl -LsSf https://astral.sh/uv/install.sh | sh # On Windows. powershell -ExecutionPolicy ByPass -c “irm https://astral.sh/uv/install.ps1 | iex” Or, from PyPI: # With pip. pip install uv # Or pipx. pipx install uv If installed via the standalone installer, uv can update itself to the latest version: uv self update See the installation documentation for details and alternative installation methods.
安装:使用我们的独立安装程序安装 uv:# 在 macOS 和 Linux 上:curl -LsSf https://astral.sh/uv/install.sh | sh # 在 Windows 上:powershell -ExecutionPolicy ByPass -c “irm https://astral.sh/uv/install.ps1 | iex”。或者,从 PyPI 安装:# 使用 pip:pip install uv # 或者 pipx:pipx install uv。如果通过独立安装程序安装,uv 可以自行更新到最新版本:uv self update。有关详细信息和其他安装方法,请参阅安装文档。
Documentation uv’s documentation is available at docs.astral.sh/uv. Additionally, the command line reference documentation can be viewed with uv help.
文档:uv 的文档可在 docs.astral.sh/uv 获取。此外,可以通过 uv help 查看命令行参考文档。
Features Projects uv manages project dependencies and environments, with support for lockfiles, workspaces, and more, similar to rye or poetry: $ uv init example Initialized project example at /home/user/example $ cd example $ uv add ruff Creating virtual environment at: .venv Resolved 2 packages in 170ms Built example @ file:///home/user/example Prepared 2 packages in 627ms Installed 2 packages in 1ms + example==0.1.0 (from file:///home/user/example) + ruff==0.5.0 $ uv run ruff check All checks passed! $ uv lock Resolved 2 packages in 0.33ms $ uv sync Resolved 2 packages in 0.70ms Checked 1 package in 0.02ms See the project documentation to get started. uv also supports building and publishing projects, even if they’re not managed with uv. See the publish guide to learn more.
功能 - 项目:uv 管理项目依赖和环境,支持锁文件、工作区等,类似于 rye 或 poetry:$ uv init example(在 /home/user/example 初始化项目 example)$ cd example $ uv add ruff(在 .venv 创建虚拟环境,170ms 内解析 2 个包,构建 example,准备 2 个包,安装 2 个包)$ uv run ruff check(所有检查通过!)$ uv lock(0.33ms 内解析 2 个包)$ uv sync(0.70ms 内解析 2 个包,检查 1 个包)。请参阅项目文档以开始使用。uv 还支持构建和发布项目,即使它们不是由 uv 管理的。请参阅发布指南以了解更多信息。
Scripts uv manages dependencies and environments for single-file scripts. Create a new script and add inline metadata declaring its dependencies: $ echo ‘import requests; print(requests.get(“https://astral.sh”))’ > example.py $ uv add —script example.py requests Updated example.py Then, run the script in an isolated virtual environment: $ uv run example.py Reading inline script metadata from: example.py Installed 5 packages in 12ms <Response [200]> See the scripts documentation to get started.
脚本:uv 为单文件脚本管理依赖和环境。创建一个新脚本并添加声明其依赖项的内联元数据:$ echo ‘import requests; print(requests.get(“https://astral.sh”))’ > example.py $ uv add —script example.py requests(更新 example.py)。然后,在隔离的虚拟环境中运行脚本:$ uv run example.py(从 example.py 读取内联脚本元数据,12ms 内安装 5 个包,<Response [200]>)。请参阅脚本文档以开始使用。
Tools uv executes and installs command-line tools provided by Python packages, similar to pipx. Run a tool in an ephemeral environment using uvx (an alias for uv tool run): $ uvx pycowsay ‘hello world!’ Resolved 1 package in 167ms Installed 1 package in 9ms + pycowsay==0.0.0.2 """ ------------ < hello world! > ------------ \ ^^ \ (oo)_____ (__)\ )/\ ||----w | || || Install a tool with uv tool install: $ uv tool install ruff Resolved 1 package in 6ms Installed 1 package in 2ms + ruff==0.5.0 Installed 1 executable: ruff $ ruff —version ruff 0.5.0 See the tools documentation to get started.
工具:uv 执行并安装由 Python 包提供的命令行工具,类似于 pipx。使用 uvx(uv tool run 的别名)在临时环境中运行工具:$ uvx pycowsay ‘hello world!’(167ms 内解析 1 个包,9ms 内安装 1 个包)。使用 uv tool install 安装工具:$ uv tool install ruff(6ms 内解析 1 个包,2ms 内安装 1 个包,安装 1 个可执行文件:ruff)。请参阅工具文档以开始使用。
Python versions uv installs Python and allows quickly switching between versions. Install multiple Python versions: $ uv python install 3.12 3.13 3.14 Installed 3 versions in 972ms + cpython-3.12.12-macos-aarch64-none (python3.12) + cpython-3.13.9-macos-aarch64-none (python3.13) + cpython-3.14.0-macos-aarch64-none (python3.14) Download Python versions as needed: $ uv venv —python 3.12.0 Using Python 3.12.0 Creating virtual environment at: .venv Activate with: source .venv/bin/activate $ uv run —python pypy@3.8 — python —version Python 3.8.16 (a9dbdca6fc3286b0addd2240f11d97d8e8de187a, Dec 29 2022, 11:45:30) [PyPy 7.3.11 with GCC Apple LLVM 13.1.6 (clang-1316.0.21.2.5)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>>> Use a specific Python version in the current directory: $ uv python pin 3.11 Pinned .python-version to 3.11 See the Python installation documentation to get started.
Python 版本:uv 安装 Python 并允许在版本之间快速切换。安装多个 Python 版本:$ uv python install 3.12 3.13 3.14(972ms 内安装 3 个版本)。按需下载 Python 版本:$ uv venv —python 3.12.0(使用 Python 3.12.0,在 .venv 创建虚拟环境,激活命令:source .venv/bin/activate)。$ uv run —python pypy@3.8 — python —version(显示 Python 3.8.16 信息)。在当前目录中使用特定 Python 版本:$ uv python pin 3.11(将 .python-version 固定为 3.11)。请参阅 Python 安装文档以开始使用。
The pip interface uv provides a drop-in replacement for common pip, pip-tools, and virtualenv commands. uv extends their interfaces with advanced features, such as dependency version overrides, platform-independent resolutions, reproducible resolutions, alternative resolution strategies, and more. Migrate to uv without changing your existing workflows — and experience a 10-100x speedup — with the uv pip interface. Compile requirements into a platform-independent requirements file: $ uv pip compile requirements.in \ —universal \ —output-file requirements.txt Resolved 43 packages in 12ms Create a virtual environment: $ uv venv Using Python 3.12.3 Creating virtual environment at: .venv Activate with: source .venv/bin/activate Install the locked requirements: $ uv pip sync requirements.txt Resolved 43 packages in 11ms Installed 43 packages in 208ms + babel==2.15.0 + black==24.4.2 + certifi==2024.7.4 … See the pip interface documentation to get started.
pip 接口:uv 为常见的 pip、pip-tools 和 virtualenv 命令提供了直接替代方案。uv 通过高级功能扩展了它们的接口,例如依赖版本覆盖、平台无关解析、可重现解析、替代解析策略等。使用 uv pip 接口迁移到 uv,无需更改现有工作流程,即可体验 10-100 倍的速度提升。将需求编译为平台无关的需求文件:$ uv pip compile requirements.in —universal —output-file requirements.txt(12ms 内解析 43 个包)。创建虚拟环境:$ uv venv(使用 Python 3.12.3,在 .venv 创建虚拟环境)。安装锁定的需求:$ uv pip sync requirements.txt(11ms 内解析 43 个包,208ms 内安装 43 个包)。请参阅 pip 接口文档以开始使用。
Contributing We are passionate about supporting contributors of all levels of experience and would love to see you get involved in the project. See the contributing guide to get started.
贡献:我们热衷于支持各种经验水平的贡献者,并期待您的参与。请参阅贡献指南以开始。
FAQ How do you pronounce uv? It’s pronounced as “you - vee” (/juː viː/) How should I stylize uv? Just “uv”, please. See the style guide for details. What platforms does uv support? See uv’s platform support document. Is uv ready for production? Yes, uv is stable and widely used in production. See uv’s versioning policy document for details.
常见问题:如何发音 uv?发音为 “you - vee” (/juː viː/)。我应该如何书写 uv 的样式?请只写 “uv”。详情请参阅样式指南。uv 支持哪些平台?请参阅 uv 的平台支持文档。uv 可以用于生产环境吗?是的,uv 很稳定,并已在生产环境中广泛使用。详情请参阅 uv 的版本控制策略文档。
Acknowledgements uv’s dependency resolver uses PubGrub under the hood. We’re grateful to the PubGrub maintainers, especially Jacob Finkelman, for their support. uv’s Git implementation is based on Cargo. Some of uv’s op
致谢:uv 的依赖解析器底层使用了 PubGrub。我们感谢 PubGrub 的维护者,特别是 Jacob Finkelman 的支持。uv 的 Git 实现基于 Cargo。uv 的部分操作…