python-uv
Original:🇺🇸 English
Translated
This skill should be used when the user asks to "set up a Python project with uv", "manage dependencies with uv", "create a uv project", "use uv for Python package management", or needs guidance on uv workflows, pyproject.toml configuration, lockfiles, and development dependency groups.
5installs
Added on
NPX Install
npx skill4agent add the-perfect-developer/the-perfect-opencode python-uvTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Python UV Project Management
Provides workflows and best practices for managing Python projects using uv — Astral's fast Python package and project manager.
Project Initialization
Create a new project with :
uv initbash
# Named project in new directory
uv init my-project
cd my-project
# Initialize in current directory
uv initGenerated structure:
my-project/
├── .gitignore
├── .python-version
├── README.md
├── main.py
└── pyproject.tomlAfter the first , , or , uv creates:
uv runuv syncuv lockmy-project/
├── .venv/
├── .python-version
├── README.md
├── main.py
├── pyproject.toml
└── uv.lockRun the project immediately after init:
bash
uv run main.pyuv auto-creates and syncs the virtual environment on every invocation.
uv runCore File Roles
| File | Purpose | Commit? |
|---|---|---|
| Broad requirements, project metadata | Yes |
| Exact resolved versions, cross-platform | Yes |
| Default Python version for the project | Yes |
| Isolated virtual environment | No |
Critical rule: Commit to version control. It guarantees reproducible installs across machines and CI environments. Never edit it manually.
uv.lockManaging Dependencies
Runtime Dependencies
bash
# Add with automatic version constraint
uv add requests
# Add with explicit version constraint
uv add 'requests>=2.31.0'
# Add with extras
uv add 'pandas[excel,plot]'
# Add platform-specific dependency
uv add "jax; sys_platform == 'linux'"Development Dependencies
Separate dev dependencies from published requirements using dependency groups:
bash
# Add to the default dev group (not published to PyPI)
uv add --dev pytest ruff mypy
# Add to named groups for fine-grained control
uv add --group lint ruff
uv add --group test pytest pytest-cov
uv add --group docs mkdocsResulting :
pyproject.tomltoml
[dependency-groups]
dev = ["pytest>=8.1.1,<9"]
lint = ["ruff>=0.4.0"]
test = ["pytest", "pytest-cov"]Groups can nest to avoid duplication:
toml
[dependency-groups]
lint = ["ruff"]
test = ["pytest"]
dev = [
{include-group = "lint"},
{include-group = "test"},
]Removing and Upgrading
bash
# Remove a dependency
uv remove requests
# Upgrade a specific package to latest compatible version
uv lock --upgrade-package requests
# Upgrade all packages
uv lock --upgradeRunning Commands
Always use — it ensures the environment is synced before execution:
uv runbash
# Run a Python script
uv run main.py
# Run a tool from the environment
uv add flask
uv run -- flask run -p 3000
# Run with a one-off extra dependency (not added to project)
uv run --with rich python -c "import rich; rich.print('[bold]Hello[/bold]')"
# Run only specific dependency groups
uv run --no-default-groups --group test pytestAlternatively, sync and activate manually when needed (e.g., interactive shells):
bash
# macOS/Linux
uv sync
source .venv/bin/activate
# Windows
uv sync
.venv\Scripts\activateDo not use for project dependencies — use instead. Direct pip manipulation bypasses the lockfile and breaks reproducibility.
uv pip installuv addDependency Sources
Override where packages are fetched from during development using :
tool.uv.sourcesbash
# From a local path (editable)
uv add --editable ../my-lib
# From a Git repository
uv add git+https://github.com/encode/httpx
# Pin to a specific Git tag
uv add git+https://github.com/encode/httpx --tag 0.27.0
# From a custom index
uv add torch --index pytorch=https://download.pytorch.org/whl/cpuSources are a uv-only development feature. Use to validate that published metadata is self-contained:
--no-sourcesbash
uv lock --no-sources
uv build --no-sourcesSyncing and Locking
bash
# Sync environment to lockfile (installs missing, removes extra)
uv sync
# Sync including all dependency groups
uv sync --all-groups
# Sync only specific groups
uv sync --group test
# Regenerate lockfile without syncing
uv lock
# Check if lockfile is up-to-date (for CI)
uv lock --checkBuilding and Viewing Version
bash
# Build source distribution and wheel
uv build
# Check built artifacts
ls dist/
# my-project-0.1.0-py3-none-any.whl
# my-project-0.1.0.tar.gz
# View current package version
uv version
uv version --short # 0.1.0 only
uv version --output-format jsonBest Practices
Lockfile hygiene
- Commit ; never edit it manually
uv.lock - Run in CI to detect uncommitted lockfile drift
uv lock --check - Use for targeted upgrades, not
uv lock --upgrade-package <name>carelessly--upgrade
Dependency discipline
- Use /
--devfor all non-runtime dependencies (linters, formatters, test runners, type checkers)--group - Never use directly in a project — always
uv pip installuv add - Set in
requires-pythonto lock down supported versionspyproject.toml
Environment hygiene
- Never commit ; the generated
.venv/excludes it by default.gitignore - Use rather than activating the venv manually in scripts and CI
uv run - For one-off tools, prefer over installing into the project environment
uvx <tool>
Publishing safety
- Run before publishing to validate published metadata is correct without
uv build --no-sourcesoverridestool.uv.sources
pyproject.toml example (library pattern):
toml
[project]
name = "my-lib"
version = "0.1.0"
description = "A short description"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"httpx>=0.27.0",
]
[project.optional-dependencies]
network = ["aiohttp>=3.9"]
[dependency-groups]
dev = [
{include-group = "lint"},
{include-group = "test"},
]
lint = ["ruff>=0.4.0", "mypy>=1.10"]
test = ["pytest>=8.0", "pytest-cov>=5.0"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"Quick Reference
| Task | Command |
|---|---|
| Create project | |
| Run script | |
| Add dependency | |
| Add dev dependency | |
| Add to named group | |
| Remove dependency | |
| Upgrade package | |
| Sync environment | |
| Rebuild lockfile | |
| Build distributions | |
| Check version | |
| Run tool one-off | |
| Simulate no sources | |
Additional Resources
Reference Files
For detailed information, consult:
- — Advanced dependency configuration: optional extras, platform markers, multiple sources, editable installs, and dependency specifier syntax
references/dependency-management.md - — Deep dive on
references/project-structure.md,pyproject.toml,uv.lock, workspace layout, and configuration options.python-version