关于 uv-package-manager
uv-package-manager 是一份介绍 uv 的 Python 项目与依赖管理指南,面向需要配置项目、虚拟环境和依赖工作流的开发者。内容涵盖锁文件、Python 版本管理及从其他工具迁移。
主要功能
- 01创建项目并管理依赖
- 02创建虚拟环境
- 03安装和固定 Python 版本
- 04生成锁文件以复现安装
- 05提供迁移与高级工作流指南
适用场景
- 01快速启动 Python 项目
- 02管理项目及开发依赖
- 03为可复现构建锁定依赖
- 04将现有项目迁移至 uv
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill uv-package-manager安装后在 Claude Code 中输入/uv-package-manager确认已加载。
附带文件references/advanced-patterns.md
详细介绍
UV 包管理器
全面介绍如何使用 uv —— 一款用 Rust 编写的极速 Python 包安装器和解析器,用于现代 Python 项目管理和依赖工作流。
何时使用本技能
- 快速搭建新的 Python 项目
- 以比 pip 更快的速度管理 Python 依赖
- 创建和管理虚拟环境
- 安装 Python 解释器
- 高效解决依赖冲突
- 从 pip/pip-tools/poetry 迁移
- 加速 CI/CD 流水线
- 管理 monorepo Python 项目
- 使用锁定文件实现可复现构建
- 优化包含 Python 依赖的 Docker 构建
核心概念
1. 什么是 uv?
- 极速包安装器:比 pip 快 10-100 倍
- 用 Rust 编写:充分利用 Rust 的性能
- 可直接替代 pip:兼容 pip 工作流
- 虚拟环境管理器:创建和管理 venv
- Python 安装器:下载和管理 Python 版本
- 解析器:高级依赖解析
- 锁定文件支持:可复现安装
2. 关键特性
- 快如闪电的安装速度
- 利用全局缓存节省磁盘空间
- 兼容 pip、pip-tools、poetry
- 全面的依赖解析
- 跨平台支持(Linux、macOS、Windows)
- 安装无需 Python
- 内置虚拟环境支持
3. UV 与传统工具对比
- 对比 pip:快 10-100 倍,解析器更好
- 对比 pip-tools:更快、更简单、体验更好
- 对比 poetry:更快、更少预设、更轻量
- 对比 conda:更快、专注 Python
安装
快速安装
# Using Homebrew (macOS or Linux)
brew install uv
# Using Cargo (if you have Rust)
cargo install --locked uv
在 Windows 上,使用 WinGet:
winget install --id=astral-sh.uv -e
其他受支持的安装方法请参阅官方安装指南。
验证安装
uv --version
# uv 0.x.x
快速开始
创建新项目
# Create new project with virtual environment
uv init my-project
cd my-project
# Or create in current directory
uv init .
# Initialize creates:
# - .python-version (Python version)
# - pyproject.toml (project config)
# - README.md
# - .gitignore
安装依赖
# Install packages (creates venv if needed)
uv add requests pandas
# Install dev dependencies
uv add --dev pytest black ruff
# Install from requirements.txt
uv pip install -r requirements.txt
# Install from pyproject.toml
uv sync
虚拟环境管理
模式 1:创建虚拟环境
# Create virtual environment with uv
uv venv
# Create with specific Python version
uv venv --python 3.12
# Create with custom name
uv venv my-env
# Create with system site packages
uv venv --system-site-packages
# Specify location
uv venv /path/to/venv
模式 2:激活虚拟环境
# Linux/macOS
source .venv/bin/activate
# Windows (Command Prompt)
.venv\Scripts\activate.bat
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# Or use uv run (no activation needed)
uv run python script.py
uv run pytest
模式 3:使用 uv run
# Run Python script (auto-activates venv)
uv run python app.py
# Run installed CLI tool
uv run black .
uv run pytest
# Run with specific Python version
uv run --python 3.11 python script.py
# Pass arguments
uv run python script.py --arg value
包管理
模式 4:添加依赖
# Add package (adds to pyproject.toml)
uv add requests
# Add with version constraint
uv add "django>=4.0,<5.0"
# Add multiple packages
uv add numpy pandas matplotlib
# Add dev dependency
uv add --dev pytest pytest-cov
# Add optional dependency group
uv add --optional docs sphinx
# Add from git
uv add git+https://github.com/user/repo.git
# Add from git with specific ref
uv add git+https://github.com/user/[email protected]
# Add from local path
uv add ./local-package
# Add editable local package
uv add -e ./local-package
模式 5:移除依赖
# Remove package
uv remove requests
# Remove dev dependency
uv remove --dev pytest
# Remove multiple packages
uv remove numpy pandas matplotlib
模式 6:升级依赖
# Upgrade specific package
uv add --upgrade requests
# Upgrade all packages
uv sync --upgrade
# Upgrade package to latest
uv add --upgrade requests
# Show what would be upgraded
uv tree --outdated
模式 7:锁定依赖
# Generate uv.lock file
uv lock
# Update lock file
uv lock --upgrade
# Lock without installing
uv lock --no-install
# Lock specific package
uv lock --upgrade-package requests
Python 版本管理
模式 8:安装 Python 版本
# Install Python version
uv python install 3.12
# Install multiple versions
uv python install 3.11 3.12 3.13
# Install latest version
uv python install
# List installed versions
uv python list
# Find available versions
uv python list --all-versions
模式 9:设置 Python 版本
# Set Python version for project
uv python pin 3.12
# This creates/updates .python-version file
# Use specific Python version for command
uv --python 3.11 run python script.py
# Create venv with specific version
uv venv --python 3.12
项目配置
模式 10:配合 uv 使用 pyproject.toml
[project]
name = "my-project"
version = "0.1.0"
description = "My awesome project"
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"requests>=2.31.0",
"pydantic>=2.0.0",
"click>=8.1.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"black>=23.0.0",
"ruff>=0.1.0",
"mypy>=1.5.0",
]
docs = [
"sphinx>=7.0.0",
"sphinx-rtd-theme>=1.3.0",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.uv]
dev-dependencies = [
# Additional dev dependencies managed by uv
]
[tool.uv.sources]
# Custom package sources
my-package = { git = "https://github.com/user/repo.git" }
模式 11:在现有项目中使用 uv
# Migrate from requirements.txt
uv add -r requirements.txt
# Migrate from poetry
# Already have pyproject.toml, just use:
uv sync
# Export to requirements.txt
uv pip freeze > requirements.txt
# Export with hashes
uv pip freeze --require-hashes > requirements.txt
有关高级工作流,包括 Docker 集成、锁定文件管理、性能优化、工具对比、常见工作流、工具集成、故障排除、最佳实践、迁移指南和命令参考,请参阅 references/advanced-patterns.md
评论
支持 Markdown