UV新一代python包管理工具
TL;DR: uv is an extremely fast Python package installer and resolver, written in Rust, and designed as a drop-in replacement for pip and pip-tools workflows.
TL;DR:uv 是一个非常快速的 Python 包安装程序和解析器,用 Rust 编写,旨在作为 pip 和pip-tools 工作流的直接替代品。
https://github.com/astral-sh/ruff
UV的优势
与其他Python
中的包管理工具相比,uv
更像是一个全能选手,它的优势在于:
速度快:得益于
Rust
,uv
工具的速度让人惊艳,比如安装依赖,速度比其他工具快很多功能全面:
uv
是“一站式服务”的工具,从安装 Python、管理虚拟环境,到安装和管理包,再到管理项目依赖,它统统都能处理得很好前景光明:背后有风投公司
Astral
支持,且采用了MIT
许可,即使未来出现问题,社区也有应对的办法
使用uv
,也可以像NodeJS
或者Rust
项目那样方便的管理依赖。
安装教程
# 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"
# With pip.
pip install uv
Requirement already satisfied: uv in /usr/local/python3/lib/python3.9/site-packages (0.6.2)
(base) [root@00gcbjih-0oqrgn1nue py-service]# uv -h
An extremely fast Python package manager.
Usage: uv [OPTIONS] <COMMAND>
Commands:
run Run a command or script
init Create a new project
add Add dependencies to the project
remove Remove dependencies from the project
sync Update the project's environment
lock Update the project's lockfile
export Export the project's lockfile to an alternate format
tree Display the project's dependency tree
tool Run and install commands provided by Python packages
python Manage Python versions and installations
pip Manage Python packages with a pip-compatible interface
venv Create a virtual environment
build Build Python packages into source distributions and wheels
publish Upload distributions to an index
cache Manage uv's cache
self Manage the uv executable
version Display uv's version
help Display documentation for a command
Cache options:
-n, --no-cache Avoid reading from or writing to the cache, instead using a temporary directory for the duration of the operation [env: UV_NO_CACHE=]
--cache-dir <CACHE_DIR> Path to the cache directory [env: UV_CACHE_DIR=]
Python options:
--python-preference <PYTHON_PREFERENCE> Whether to prefer uv-managed or system Python installations [env: UV_PYTHON_PREFERENCE=] [possible values: only-managed, managed, system, only-system]
--no-python-downloads Disable automatic downloads of Python. [env: "UV_PYTHON_DOWNLOADS=never"]
Global options:
-q, --quiet Do not print any output
-v, --verbose... Use verbose output
--color <COLOR_CHOICE> Control the use of color in output [possible values: auto, always, never]
--native-tls Whether to load TLS certificates from the platform's native certificate store [env: UV_NATIVE_TLS=]
--offline Disable network access [env: UV_OFFLINE=]
--allow-insecure-host <ALLOW_INSECURE_HOST> Allow insecure connections to a host [env: UV_INSECURE_HOST=]
--no-progress Hide all progress outputs [env: UV_NO_PROGRESS=]
--directory <DIRECTORY> Change to the given directory prior to running the command
--project <PROJECT> Run the command within the given project directory
--config-file <CONFIG_FILE> The path to a `uv.toml` file to use for configuration [env: UV_CONFIG_FILE=]
--no-config Avoid discovering configuration files (`pyproject.toml`, `uv.toml`) [env: UV_NO_CONFIG=]
-h, --help Display the concise help for this command
-V, --version Display the uv version
Use `uv help` for more details.
使用演示
初始化项目
pyproject.toml
:定义项目的主要依赖,包括项目名称、版本、描述、支持的Python
版本等信息uv.lock
:记录项目的所有依赖,包括依赖的依赖,且跨平台,确保在不同环境下安装的一致性。这个文件由uv
自动管理,不要手动编辑
(base) [root@00gcbjih-0oqrgn1nue code]# uv init py-service
Initialized project `py-service` at `/mnt/vdb/code/py-service`
(base) [root@00gcbjih-0oqrgn1nue py-service]# ll -rht
total 8.0K
-rw-r--r--. 1 root root 155 Mar 1 18:09 pyproject.toml
-rw-r--r--. 1 root root 88 Mar 1 18:09 main.py
-rw-r--r--. 1 root root 0 Mar 1 18:09 README.md
依赖管理
以pandas为例,演示用过uv管理项目依赖
(base) [root@00gcbjih-0oqrgn1nue py-service]# uv add pandas --index-url https://pypi.tuna.tsinghua.edu.cn/simple/
warning: Indexes specified via `--index-url` will not be persisted to the `pyproject.toml` file; use `--default-index` instead.
Resolved 8 packages in 1.38s
Prepared 4 packages in 1.30s
░░░░░░░░░░░░░░░░░░░░ [0/6] Installing wheels... warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
If the cache and target directories are on different filesystems, hardlinking may not be supported.
If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
Installed 6 packages in 360ms
+ numpy==2.0.2
+ pandas==2.2.3
+ python-dateutil==2.9.0.post0
+ pytz==2025.1
+ six==1.17.0
操作环境
(base) [root@00gcbjih-0oqrgn1nue py-service]# uv sync
Resolved 8 packages in 4ms
Audited 6 packages in 0.06ms
同步之后,会自动查找或下载合适的 Python
版本,创建并设置项目的虚拟环境,构建完整的依赖列表并写入
uv.lock
文件,最后将依赖同步到虚拟环境中。
之后就可以运行项目的代码了
环境管理
比较常用的功能是区分开发环境和生产环境的依赖
uv add --group production requests
uv add --group dev pandas
评论区