755 字
4 分钟
UV 学习记录
UV 是什么?
UV 是一个用 Rust 编写的快速 Python 包/项目管理工具,旨在替代 pip、pip-tools、pipx、poetry、pyenv、virtualenv 等工具,提供统一而高效的体验。
安装
Linux/macOS
curl -LsSf https://astral.sh/uv/install.sh | shWindows(推荐)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"通过 pip 安装
pip install uv基本使用速查
1) 创建项目与环境
# 新建并初始化项目uv init my-projectcd my-project
# 在现有目录初始化uv init
# 指定 Python 版本uv init --python 3.11 my-project2) 管理 Python 环境
# 创建虚拟环境(自动检测 Python)uv venv
# 指定 Python 版本uv venv --python 3.11
# 指定虚拟环境名称(如需)uv venv my-env
# 激活(传统方式)# Linux/macOS: source .venv/bin/activate# Windows: .venv\Scripts\activate
# 推荐:无需显式激活,直接运行uv run python script.py
# 停用(传统方式)deactivate3) 包管理
# 安装包uv add requestsuv add requests pandas numpyuv add --dev pytest blackuv add "requests>=2.28.0"uv add "pandas==1.5.0"
# 从 requirements.txt 安装uv add -r requirements.txt
# 移除包uv remove requestsuv remove requests pandas
# 更新uv lock --upgrade # 全部包uv add requests --upgrade # 指定包4) 运行代码
# 脚本/模块uv run python script.pyuv run python -m pytest
# 带参数uv run python script.py --arg value
# 一次性脚本(临时环境安装后运行)uv run --with requests python -c "import requests; print(requests.get('https://httpbin.org/get').json())"5) requirements.txt 管理
# 由当前环境导出uv pip freeze > requirements.txt
# 或直接导出uv export --format requirements-txt --output-file requirements.txt
# 从 requirements.txt 安装uv add -r requirements.txt# 或(在虚拟环境中)uv pip install -r requirements.txt若需手动维护依赖,UV 主要使用 pyproject.toml:
[project]name = "my-project"version = "0.1.0"dependencies = [ "requests>=2.28.0", "pandas>=1.5.0", "numpy>=1.24.0",]
[project.optional-dependencies]dev = [ "pytest>=7.0.0", "black>=22.0.0", "flake8>=5.0.0",]
[build-system]requires = ["hatchling"]build-backend = "hatchling.build"6) 锁定依赖
# 生成/更新锁定文件 uv.lockuv lockuv lock --upgrade
# 按锁定版本安装uv syncuv sync --no-dev # 仅生产依赖7) Python 版本管理
uv python listuv python install 3.11uv python install 3.12uv run --python 3.11 python script.py8) 常用工作流
# 新项目uv init my-project && cd my-projectuv add requests pandasuv add --dev pytest blackuv run python main.py
# 克隆现有项目git clone <repo> && cd <dir>uv syncuv run python main.py9) 高级功能
# 工具(类似 pipx)uv tool run black .uv tool run pytestuv tool install blackuv tool install pytest
# 缓存uv cache infouv cache clean
# 配置uv config listuv config set index-url https://pypi.org/simple/10) 迁移指南
# 从 pip + requirements.txt 迁移uv inituv add -r requirements.txtrm requirements.txt # 可选
# 从 Poetry 迁移uv inituv sync11) 设置超时时间
UV_HTTP_TIMEOUT=60 uv sync常见问题(FAQ)
在 CI/CD 中使用 UV
- name: Install uv uses: astral-sh/setup-uv@v1
- name: Install dependencies run: uv sync
- name: Run tests run: uv run pytest配置私有源
uv config set index-url https://private-pypi.company.com/simple/或在 pyproject.toml:
[[tool.uv.index]]name = "private"url = "https://private-pypi.company.com/simple/"查看已安装的包
uv pip listuv pip show requests换源示例
# 阿里云export UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
# 清华export UV_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple/
# 中科大export UV_INDEX_URL=https://mirrors.ustc.edu.cn/pypi/simple/
# 使用示例uv pip install <package-name>最佳实践
- 使用
uv run代替手动激活环境 - 提交
uv.lock,确保团队一致性 - 以
pyproject.toml为中心管理依赖 - 定期
uv lock --upgrade更新依赖 - 使用
--dev区分开发/生产依赖
参考
- UV 官方文档
- UV GitHub 仓库
- Python 打包用户指南