Python 包管理工具 uv
uv 是由 Rust 编写的超快速 Python 包安装器和解析器,由创建了流行的 Rust 包管理器 Cargo 的团队开发。它旨在成为未来 pip 和 pip-tools 的现代替代品。
主要特性
- 极快的性能:比 pip 快 10-100 倍
- 现代功能:
- 支持最新的 Python 包标准
- 内置虚拟环境管理
- 支持锁定文件
- 兼容性:
- 兼容现有的 pip 和 pip-tools 工作流
- 支持 requirements.txt 和 pyproject.toml
uv 安装
$powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Downloading uv 0.7.19 (x86_64-pc-windows-msvc)
Installing to C:\Users\user\.local\binuv.exeuvx.exeuvw.exe
everything's installed!To add C:\Users\user\.local\bin to your PATH, either restart your shell or run:set Path=C:\Users\user\.local\bin;%Path% (cmd)$env:Path = "C:\Users\user\.local\bin;$env:Path" (powershell)
主要功能
uv -h
An extremely fast Python package manager.Usage: uv [OPTIONS] <COMMAND>Commands:run Run a command or scriptinit Create a new projectadd Add dependencies to the projectremove Remove dependencies from the projectversion Read or update the project's versionsync Update the project's environmentlock Update the project's lockfileexport Export the project's lockfile to an alternate formattree Display the project's dependency treetool Run and install commands provided by Python packagespython Manage Python versions and installationspip Manage Python packages with a pip-compatible interfacevenv Create a virtual environmentbuild Build Python packages into source distributions and wheelspublish Upload distributions to an indexcache Manage uv's cacheself Manage the uv executablehelp Display documentation for a command
Python 版本管理
Commands:list List the available Python installationsinstall Download and install Python versionsupgrade Upgrade installed Python versions to the latest supported patch release (requires the `--preview` flag)find Search for a Python installationpin Pin to a specific Python versiondir Show the uv Python installation directoryuninstall Uninstall Python versions
包安装
uv pip install flask # 安装单个包
uv pip install -r requirements.txt # 从文件安装
虚拟环境管理
uv venv .venv # 创建虚拟环境
source .venv/bin/activate # 激活虚拟环境
依赖解析
uv pip compile pyproject.toml -o requirements.txt # 生成锁定文件
uv pip sync requirements.txt # 同步依赖
uv 目前仍处于积极开发阶段,但已经可以用于生产环境,特别适合需要频繁安装Python包或大型项目的开发者。
相关链接
https://docs.astral.sh/uv/