AgentSkillsCN

uv-advanced

Astral出品的极速Python包和项目管理器uv的高级用法。当使用uv进行项目管理(uv init、uv add、uv run、uv lock、uv sync)、工作区和monorepos、依赖解析策略(通用、平台特定、约束、覆盖)、Docker容器化、PEP 723内联脚本元数据、uvx工具执行、Python版本管理、pip接口迁移、pyproject.toml配置或任何uv高级工作流时使用此技能。涵盖工作区、解析策略、Docker最佳实践、CI/CD集成和从pip/poetry/pipenv迁移。

SKILL.md
--- frontmatter
name: uv-advanced
description: Advanced usage of uv, the extremely fast Python package and project manager from Astral. Use this skill when working with uv for project management (uv init, uv add, uv run, uv lock, uv sync), workspaces and monorepos, dependency resolution strategies (universal, platform-specific, constraints, overrides), Docker containerization, PEP 723 inline script metadata, uvx tool execution, Python version management, pip interface migration, pyproject.toml configuration, or any advanced uv workflow. Covers workspaces, resolution strategies, Docker best practices, CI/CD integration, and migration from pip/poetry/pipenv.

uv Advanced Usage

uv is an extremely fast Python package and project manager written in Rust. It replaces pip, pip-tools, pipx, poetry, pyenv, virtualenv, and more with a single unified tool that's 10-100x faster.

Quick Reference

TaskCommand
Create projectuv init myproject
Add dependencyuv add requests
Add dev dependencyuv add --dev pytest
Run commanduv run python main.py
Lock dependenciesuv lock
Sync environmentuv sync
Run tooluvx ruff check .
Install Pythonuv python install 3.12

Core Concepts

Project Structure

code
myproject/
├── pyproject.toml    # Project metadata and dependencies
├── uv.lock           # Universal lockfile (commit this)
├── .venv/            # Virtual environment (gitignore)
└── src/
    └── myproject/

pyproject.toml Essentials

toml
[project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["requests>=2.28", "rich"]

[project.optional-dependencies]
dev = ["pytest", "ruff"]

[dependency-groups]
test = ["pytest>=8.0", "pytest-cov"]

[tool.uv]
dev-dependencies = ["ruff", "mypy"]

[tool.uv.sources]
# Use git dependency during development
mylib = { git = "https://github.com/org/mylib", branch = "main" }
# Use workspace member
shared = { workspace = true }
# Use local path
utils = { path = "../utils", editable = true }

Reference Documentation

For detailed guidance on specific topics:

  • Projects — Project lifecycle: init, add, run, lock, sync, build, publish
  • Workspaces — Monorepo management with shared lockfiles
  • Resolution — Universal resolution, constraints, overrides, conflict handling
  • Docker — Container images, multi-stage builds, cache optimization
  • Scripts & Tools — PEP 723 inline metadata, uvx, tool management
  • Python Versions — Installing and managing Python interpreters
  • Configuration — pyproject.toml, uv.toml, environment variables
  • pip Interface — Drop-in pip replacement with advanced features

Common Workflows

Start a New Project

bash
uv init myproject
cd myproject
uv add fastapi uvicorn
uv run uvicorn main:app --reload

Migrate from requirements.txt

bash
uv init
uv add -r requirements.txt
uv lock

Create Reproducible Builds

bash
# Lock with timestamp for reproducibility
uv lock --exclude-newer "2025-01-01"

# Export for pip compatibility
uv export --frozen > requirements.txt

Test Against Lowest Bounds

bash
uv run --resolution lowest pytest

Key Flags

FlagPurpose
--frozenUse exact lockfile versions, fail if outdated
--lockedUse lockfile, fail if missing or outdated
--no-devExclude development dependencies
--all-extrasInclude all optional dependencies
--upgradeAllow upgrading locked dependencies
--resolution lowestUse lowest compatible versions
--universalCreate platform-independent resolution (pip compile)