uv-package-manager by wshobson/agents
npx skills add https://github.com/wshobson/agents --skill uv-package-manager关于使用 uv 的全面指南,uv 是一个用 Rust 编写的极速 Python 包安装器和解析器,适用于现代 Python 项目管理和依赖工作流。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# 使用 pip(如果已有 Python 环境)
pip install uv
# 使用 Homebrew (macOS)
brew install uv
# 使用 cargo(如果有 Rust 环境)
cargo install --git https://github.com/astral-sh/uv uv
uv --version
# uv 0.x.x
# 创建带有虚拟环境的新项目
uv init my-project
cd my-project
# 或在当前目录创建
uv init .
# 初始化会创建:
# - .python-version (Python 版本)
# - pyproject.toml (项目配置)
# - README.md
# - .gitignore
# 安装包(如果需要会创建 venv)
uv add requests pandas
# 安装开发依赖
uv add --dev pytest black ruff
# 从 requirements.txt 安装
uv pip install -r requirements.txt
# 从 pyproject.toml 安装
uv sync
# 使用 uv 创建虚拟环境
uv venv
# 使用特定 Python 版本创建
uv venv --python 3.12
# 使用自定义名称创建
uv venv my-env
# 使用系统站点包创建
uv venv --system-site-packages
# 指定位置
uv venv /path/to/venv
# Linux/macOS
source .venv/bin/activate
# Windows (命令提示符)
.venv\Scripts\activate.bat
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# 或使用 uv run(无需激活)
uv run python script.py
uv run pytest
# 运行 Python 脚本(自动激活 venv)
uv run python app.py
# 运行已安装的 CLI 工具
uv run black .
uv run pytest
# 使用特定 Python 版本运行
uv run --python 3.11 python script.py
# 传递参数
uv run python script.py --arg value
# 添加包(添加到 pyproject.toml)
uv add requests
# 添加带版本约束的包
uv add "django>=4.0,<5.0"
# 添加多个包
uv add numpy pandas matplotlib
# 添加开发依赖
uv add --dev pytest pytest-cov
# 添加可选依赖组
uv add --optional docs sphinx
# 从 git 添加
uv add git+https://github.com/user/repo.git
# 从 git 添加特定引用
uv add git+https://github.com/user/repo.git@v1.0.0
# 从本地路径添加
uv add ./local-package
# 添加可编辑的本地包
uv add -e ./local-package
# 移除包
uv remove requests
# 移除开发依赖
uv remove --dev pytest
# 移除多个包
uv remove numpy pandas matplotlib
# 升级特定包
uv add --upgrade requests
# 升级所有包
uv sync --upgrade
# 升级包到最新版本
uv add --upgrade requests
# 显示可升级的包
uv tree --outdated
# 生成 uv.lock 文件
uv lock
# 更新锁文件
uv lock --upgrade
# 锁定但不安装
uv lock --no-install
# 锁定特定包
uv lock --upgrade-package requests
# 安装 Python 版本
uv python install 3.12
# 安装多个版本
uv python install 3.11 3.12 3.13
# 安装最新版本
uv python install
# 列出已安装版本
uv python list
# 查找可用版本
uv python list --all-versions
# 为项目设置 Python 版本
uv python pin 3.12
# 这会创建/更新 .python-version 文件
# 为命令使用特定 Python 版本
uv --python 3.11 run python script.py
# 使用特定版本创建 venv
uv venv --python 3.12
[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 = [
# uv 管理的额外开发依赖
]
[tool.uv.sources]
# 自定义包源
my-package = { git = "https://github.com/user/repo.git" }
# 从 requirements.txt 迁移
uv add -r requirements.txt
# 从 poetry 迁移
# 已有 pyproject.toml,只需使用:
uv sync
# 导出到 requirements.txt
uv pip freeze > requirements.txt
# 带哈希值导出
uv pip freeze --require-hashes > requirements.txt
# 项目结构
# monorepo/
# packages/
# package-a/
# pyproject.toml
# package-b/
# pyproject.toml
# pyproject.toml (根目录)
# 根目录 pyproject.toml
[tool.uv.workspace]
members = ["packages/*"]
# 安装所有工作区包
uv sync
# 添加工作区依赖
uv add --path ./packages/package-a
# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v2
with:
enable-cache: true
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync --all-extras --dev
- name: Run tests
run: uv run pytest
- name: Run linting
run: |
uv run ruff check .
uv run black --check .
# Dockerfile
FROM python:3.12-slim
# 安装 uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# 设置工作目录
WORKDIR /app
# 复制依赖文件
COPY pyproject.toml uv.lock ./
# 安装依赖
RUN uv sync --frozen --no-dev
# 复制应用程序代码
COPY . .
# 运行应用程序
CMD ["uv", "run", "python", "app.py"]
优化的多阶段构建:
# 多阶段 Dockerfile
FROM python:3.12-slim AS builder
# 安装 uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
WORKDIR /app
# 安装依赖到 venv
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-editable
# 运行时阶段
FROM python:3.12-slim
WORKDIR /app
# 从构建器复制 venv
COPY --from=builder /app/.venv .venv
COPY . .
# 使用 venv
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "app.py"]
# 创建锁文件 (uv.lock)
uv lock
# 从锁文件安装(精确版本)
uv sync --frozen
# 更新锁文件但不安装
uv lock --no-install
# 升级锁文件中的特定包
uv lock --upgrade-package requests
# 检查锁文件是否最新
uv lock --check
# 导出锁文件到 requirements.txt
uv export --format requirements-txt > requirements.txt
# 为安全起见带哈希值导出
uv export --format requirements-txt --hash > requirements.txt
# UV 自动使用全局缓存位置:
# Linux: ~/.cache/uv
# macOS: ~/Library/Caches/uv
# Windows: %LOCALAPPDATA%\uv\cache
# 清除缓存
uv cache clean
# 检查缓存大小
uv cache dir
# UV 默认并行安装包
# 控制并行度
uv pip install --jobs 4 package1 package2
# 无并行(顺序)
uv pip install --jobs 1 package
# 仅从缓存安装(无网络)
uv pip install --offline package
# 离线从锁文件同步
uv sync --frozen --offline
# pip
python -m venv .venv
source .venv/bin/activate
pip install requests pandas numpy
# ~30 秒
# uv
uv venv
uv add requests pandas numpy
# ~2 秒 (快 10-15 倍)
# poetry
poetry init
poetry add requests pandas
poetry install
# ~20 秒
# uv
uv init
uv add requests pandas
uv sync
# ~3 秒 (快 6-7 倍)
# pip-tools
pip-compile requirements.in
pip-sync requirements.txt
# ~15 秒
# uv
uv lock
uv sync --frozen
# ~2 秒 (快 7-8 倍)
# 完整工作流
uv init my-project
cd my-project
# 设置 Python 版本
uv python pin 3.12
# 添加依赖
uv add fastapi uvicorn pydantic
# 添加开发依赖
uv add --dev pytest black ruff mypy
# 创建结构
mkdir -p src/my_project tests
# 运行测试
uv run pytest
# 格式化代码
uv run black .
uv run ruff check .
# 克隆仓库
git clone https://github.com/user/project.git
cd project
# 安装依赖(自动创建 venv)
uv sync
# 安装开发依赖
uv sync --all-extras
# 更新依赖
uv lock --upgrade
# 运行应用程序
uv run python app.py
# 运行测试
uv run pytest
# 添加新依赖
uv add new-package
# 提交更新的文件
git add pyproject.toml uv.lock
git commit -m "Add new-package dependency"
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: uv-lock
name: uv lock
entry: uv lock
language: system
pass_filenames: false
- id: ruff
name: ruff
entry: uv run ruff check --fix
language: system
types: [python]
- id: black
name: black
entry: uv run black
language: system
types: [python]
// .vscode/settings.json
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.terminal.activateEnvironment": true,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": ["-v"],
"python.linting.enabled": true,
"python.formatting.provider": "black",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
}
# 问题:找不到 uv
# 解决方案:添加到 PATH 或重新安装
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.bashrc
# 问题:错误的 Python 版本
# 解决方案:显式固定版本
uv python pin 3.12
uv venv --python 3.12
# 问题:依赖冲突
# 解决方案:检查解析
uv lock --verbose
# 问题:缓存问题
# 解决方案:清除缓存
uv cache clean
# 问题:锁文件不同步
# 解决方案:重新生成
uv lock --upgrade
# 在 CI 中使用冻结安装
uv sync --frozen
# 尽可能使用离线模式
uv sync --offline
# 并行操作(自动)
# uv 默认执行此操作
# 跨环境重用缓存
# uv 全局共享缓存
# 使用锁文件跳过解析
uv sync --frozen # 跳过解析
# 之前
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# 之后
uv venv
uv pip install -r requirements.txt
# 或更好:
uv init
uv add -r requirements.txt
# 之前
poetry install
poetry add requests
# 之后
uv sync
uv add requests
# 保留现有的 pyproject.toml
# uv 读取 [project] 和 [tool.poetry] 部分
# 之前
pip-compile requirements.in
pip-sync requirements.txt
# 之后
uv lock
uv sync --frozen
# 项目管理
uv init [PATH] # 初始化项目
uv add PACKAGE # 添加依赖
uv remove PACKAGE # 移除依赖
uv sync # 安装依赖
uv lock # 创建/更新锁文件
# 虚拟环境
uv venv [PATH] # 创建 venv
uv run COMMAND # 在 venv 中运行
# Python 管理
uv python install VERSION # 安装 Python
uv python list # 列出已安装的 Python
uv python pin VERSION # 固定 Python 版本
# 包安装(兼容 pip)
uv pip install PACKAGE # 安装包
uv pip uninstall PACKAGE # 卸载包
uv pip freeze # 列出已安装
uv pip list # 列出包
# 实用工具
uv cache clean # 清除缓存
uv cache dir # 显示缓存位置
uv --version # 显示版本
每周安装量
4.5K
仓库
GitHub 星标数
32.2K
首次出现
2026年1月20日
安全审计
已安装于
opencode3.5K
gemini-cli3.4K
codex3.3K
claude-code3.2K
cursor3.1K
github-copilot2.9K
Comprehensive guide to using uv, an extremely fast Python package installer and resolver written in Rust, for modern Python project management and dependency workflows.
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Using pip (if you already have Python)
pip install uv
# Using Homebrew (macOS)
brew install uv
# Using cargo (if you have Rust)
cargo install --git https://github.com/astral-sh/uv uv
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
# 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
# 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
# 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
# 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/repo.git@v1.0.0
# Add from local path
uv add ./local-package
# Add editable local package
uv add -e ./local-package
# Remove package
uv remove requests
# Remove dev dependency
uv remove --dev pytest
# Remove multiple packages
uv remove numpy pandas matplotlib
# 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
# 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
# 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
# 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
[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" }
# 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
# Project structure
# monorepo/
# packages/
# package-a/
# pyproject.toml
# package-b/
# pyproject.toml
# pyproject.toml (root)
# Root pyproject.toml
[tool.uv.workspace]
members = ["packages/*"]
# Install all workspace packages
uv sync
# Add workspace dependency
uv add --path ./packages/package-a
# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v2
with:
enable-cache: true
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync --all-extras --dev
- name: Run tests
run: uv run pytest
- name: Run linting
run: |
uv run ruff check .
uv run black --check .
# Dockerfile
FROM python:3.12-slim
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# Set working directory
WORKDIR /app
# Copy dependency files
COPY pyproject.toml uv.lock ./
# Install dependencies
RUN uv sync --frozen --no-dev
# Copy application code
COPY . .
# Run application
CMD ["uv", "run", "python", "app.py"]
Optimized multi-stage build:
# Multi-stage Dockerfile
FROM python:3.12-slim AS builder
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
WORKDIR /app
# Install dependencies to venv
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-editable
# Runtime stage
FROM python:3.12-slim
WORKDIR /app
# Copy venv from builder
COPY --from=builder /app/.venv .venv
COPY . .
# Use venv
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "app.py"]
# Create lockfile (uv.lock)
uv lock
# Install from lockfile (exact versions)
uv sync --frozen
# Update lockfile without installing
uv lock --no-install
# Upgrade specific package in lock
uv lock --upgrade-package requests
# Check if lockfile is up to date
uv lock --check
# Export lockfile to requirements.txt
uv export --format requirements-txt > requirements.txt
# Export with hashes for security
uv export --format requirements-txt --hash > requirements.txt
# UV automatically uses global cache at:
# Linux: ~/.cache/uv
# macOS: ~/Library/Caches/uv
# Windows: %LOCALAPPDATA%\uv\cache
# Clear cache
uv cache clean
# Check cache size
uv cache dir
# UV installs packages in parallel by default
# Control parallelism
uv pip install --jobs 4 package1 package2
# No parallel (sequential)
uv pip install --jobs 1 package
# Install from cache only (no network)
uv pip install --offline package
# Sync from lockfile offline
uv sync --frozen --offline
# pip
python -m venv .venv
source .venv/bin/activate
pip install requests pandas numpy
# ~30 seconds
# uv
uv venv
uv add requests pandas numpy
# ~2 seconds (10-15x faster)
# poetry
poetry init
poetry add requests pandas
poetry install
# ~20 seconds
# uv
uv init
uv add requests pandas
uv sync
# ~3 seconds (6-7x faster)
# pip-tools
pip-compile requirements.in
pip-sync requirements.txt
# ~15 seconds
# uv
uv lock
uv sync --frozen
# ~2 seconds (7-8x faster)
# Complete workflow
uv init my-project
cd my-project
# Set Python version
uv python pin 3.12
# Add dependencies
uv add fastapi uvicorn pydantic
# Add dev dependencies
uv add --dev pytest black ruff mypy
# Create structure
mkdir -p src/my_project tests
# Run tests
uv run pytest
# Format code
uv run black .
uv run ruff check .
# Clone repository
git clone https://github.com/user/project.git
cd project
# Install dependencies (creates venv automatically)
uv sync
# Install with dev dependencies
uv sync --all-extras
# Update dependencies
uv lock --upgrade
# Run application
uv run python app.py
# Run tests
uv run pytest
# Add new dependency
uv add new-package
# Commit updated files
git add pyproject.toml uv.lock
git commit -m "Add new-package dependency"
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: uv-lock
name: uv lock
entry: uv lock
language: system
pass_filenames: false
- id: ruff
name: ruff
entry: uv run ruff check --fix
language: system
types: [python]
- id: black
name: black
entry: uv run black
language: system
types: [python]
// .vscode/settings.json
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.terminal.activateEnvironment": true,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": ["-v"],
"python.linting.enabled": true,
"python.formatting.provider": "black",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
}
# Issue: uv not found
# Solution: Add to PATH or reinstall
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.bashrc
# Issue: Wrong Python version
# Solution: Pin version explicitly
uv python pin 3.12
uv venv --python 3.12
# Issue: Dependency conflict
# Solution: Check resolution
uv lock --verbose
# Issue: Cache issues
# Solution: Clear cache
uv cache clean
# Issue: Lockfile out of sync
# Solution: Regenerate
uv lock --upgrade
# Use frozen installs in CI
uv sync --frozen
# Use offline mode when possible
uv sync --offline
# Parallel operations (automatic)
# uv does this by default
# Reuse cache across environments
# uv shares cache globally
# Use lockfiles to skip resolution
uv sync --frozen # skips resolution
# Before
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# After
uv venv
uv pip install -r requirements.txt
# Or better:
uv init
uv add -r requirements.txt
# Before
poetry install
poetry add requests
# After
uv sync
uv add requests
# Keep existing pyproject.toml
# uv reads [project] and [tool.poetry] sections
# Before
pip-compile requirements.in
pip-sync requirements.txt
# After
uv lock
uv sync --frozen
# Project management
uv init [PATH] # Initialize project
uv add PACKAGE # Add dependency
uv remove PACKAGE # Remove dependency
uv sync # Install dependencies
uv lock # Create/update lockfile
# Virtual environments
uv venv [PATH] # Create venv
uv run COMMAND # Run in venv
# Python management
uv python install VERSION # Install Python
uv python list # List installed Pythons
uv python pin VERSION # Pin Python version
# Package installation (pip-compatible)
uv pip install PACKAGE # Install package
uv pip uninstall PACKAGE # Uninstall package
uv pip freeze # List installed
uv pip list # List packages
# Utility
uv cache clean # Clear cache
uv cache dir # Show cache location
uv --version # Show version
Weekly Installs
4.5K
Repository
GitHub Stars
32.2K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketWarnSnykPass
Installed on
opencode3.5K
gemini-cli3.4K
codex3.3K
claude-code3.2K
cursor3.1K
github-copilot2.9K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装