This guide covers essential uv workflows including dependency management, Python version control, and project initialization. It assumes you have basic Python knowledge.
Abstract
UV is an extremely fast Python package and project manager written in Rust by Astral. It serves as a drop-in replacement for pip, pip-tools, virtualenv, and even pyenv. UV dramatically improves performance (10-100x faster than pip) while maintaining compatibility with existing Python packaging standards like requirements.txt and pyproject.toml.
This document provides a comprehensive guide to UV fundamentals, covering dependency management, Python version management, and modern project workflows.
Key Features:
- ⚡ 10-100x faster than pip
- 🐍 Built-in Python version management
- 📦 Modern pyproject.toml support
- 🔒 Deterministic dependency resolution
pyproject.toml follows the PEP 621 standard for Python project metadata. It consolidates project configuration, dependencies, and tool settings in a single file.
description="Short description"readme="README.md"# Can also be {file = "README.md", content-type = "text/markdown"}requires-python=">=3.11"# Minimum Python versionlicense={text="MIT"}# Or {file = "LICENSE"}authors=[{name="Name",email="email@example.com"}]maintainers=[{name="Maintainer",email="maint@example.com"}]keywords=["web","api"]# PyPI search keywordsclassifiers=[# PyPI classifiers"Development Status :: 4 - Beta","License :: OSI Approved :: MIT License",]
dependencies=["package-name",# Latest version"package>=1.0",# Minimum version"package>=1.0,<2.0",# Version range"package~=1.4.2",# Compatible release (~= is >=1.4.2, <1.5.0)"package[extra]",# With extras"package @ https://example.com",# Direct URL]
[tool.uv]dev-dependencies=["ipython>=8.0.0","ipdb>=0.13.0",][tool.uv.sources]# Use specific package sourcesmy-package={git="https://github.com/user/repo.git"}another-package={path="../local-package",editable=true}
[tool.ruff]# General settingsline-length=88target-version="py311"exclude=[".git",".venv","__pycache__","build","dist",]# Linting rulesselect=["E",# pycodestyle errors"F",# pyflakes"I",# isort"N",# pep8-naming"W",# pycodestyle warnings]ignore=["E501"]# Line too long[tool.ruff.lint]extend-select=["Q","RUF100","C90"]mccabe.max-complexity=14[tool.ruff.format]quote-style="double"indent-style="space"[tool.ruff.lint.isort]known-first-party=["myproject"]
[tool.pytest.ini_options]# Test discoverytestpaths=["tests"]python_files=["test_*.py","*_test.py"]python_functions=["test_*"]python_classes=["Test*"]# Optionsaddopts=["-v",# Verbose"--strict-markers",# Strict marker usage"--cov=myproject",# Coverage for myproject"--cov-report=html",# HTML coverage report"--cov-report=term-missing",# Show missing lines]# Markersmarkers=["slow: marks tests as slow","integration: marks tests as integration tests",]
uvsync# Install production dependenciesuvsync--extradev# Install with dev dependenciesuvsync--extradev--extradocs# Install multiple groupsuvsync--all-extras# Install all optional dependencies
# Run tests with pytest configuration from pyproject.tomluvrunpytest
# Format code with black configurationuvrunblack.
# Lint with ruff configurationuvrunruffcheck.
# Type check with mypy configurationuvrunmypysrc/
uvaddpandas# Add pandas to dependenciesuvadd"numpy>=1.24"# Add with version constraintuvadd--devpytest# Add to dev dependenciesuvadd"flask[async]"# Add with extras
Note
uv add automatically updates pyproject.toml and installs the package.
requirements.in is a file that contains all the dependencies of your project. It is used to specify the exact versions of the dependencies that you want to use. This is important because it ensures that the project is reproducible. If you don't specify the exact versions of the dependencies, you may get different results when running the project on different machines.
What is requirements.txt?
requirements.txt is a file that contains all the dependencies of your project. It is used to specify the exact versions of the dependencies that you want to use. This is important because it ensures that the project is reproducible. If you don't specify the exact versions of the dependencies, you may get different results when running the project on different machines.
uvpipinstall<package># Install single packageuvpipinstall-rrequirements.txt# Install from requirements fileuvpipinstall-e.# Install project in editable modeuvpipuninstall<package># Uninstall packageuvpiplist# List installed packagesuvpipfreeze# Output installed packagesuvpipshow<package># Show package information
uvpipcompilerequirements.in-orequirements.txt# Compile requirementsuvpipcompilerequirements.in--upgrade# Upgrade all packagesuvpipcompilerequirements.in--upgrade-packagepandas# Upgrade specific packageuvpipsyncrequirements.txt# Sync environment to exact requirements
uvpythonlist# List available Python versionsuvpythoninstall3.12# Install Python versionuvpythonpin3.12# Pin project to Python versionuvpythonfind# Show which Python will be used
#!/bin/bashset-e# Exit on errorecho"Creating virtual environment..."uvvenv
echo"Compiling production dependencies..."uvpipcompilerequirements.in-orequirements.txt
echo"Installing production dependencies..."uvpipinstall-rrequirements.txt
echo"Compiling development dependencies..."uvpipcompiledev-requirements.in-odev-requirements.txt
echo"Installing development dependencies..."uvpipinstall-rdev-requirements.txt
echo""echo"Environment setup complete!"echo"To activate: source .venv/bin/activate"
# Virtual environment
.venv/
venv/
env/
# Python cache
__pycache__/
*.py[cod]
*.pyo
*.pyd
*.so
.Python
# Distribution / packaging
build/
dist/
*.egg-info/
.eggs/
# UV cache
.uv/
# Logs and local config
*.log
.env
.env.local
# IDE files
.vscode/
.idea/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Lockfiles (uncomment if you want to ignore them)
# requirements.txt
# dev-requirements.txt
# uv.lock
For Team Projects
Do commitrequirements.txt, dev-requirements.txt, and uv.lock to ensure all team members use identical dependency versions. Do ignore.venv/ and cache directories.
# 1. Install UVpipxinstalluv
# 2. Create requirements.in from existing requirements.txt# (manually extract only direct dependencies)# 3. Compile with UVuvpipcompilerequirements.in-orequirements.txt
# 4. Create new environmentuvvenv
# 5. Install with UVuvpipinstall-rrequirements.txt
UV revolutionizes Python package and project management by providing:
Speed: 10-100x faster than traditional tools
Python Version Management: Built-in Python installation and management
Modern Standards: First-class support for pyproject.toml (PEP 621)
Compatibility: Drop-in replacement for pip, pip-tools, and virtualenv
Reliability: Deterministic dependency resolution with lockfiles
Whether you're starting a new project with pyproject.toml or maintaining an existing one with requirements.txt, UV provides the performance and reliability you need for modern Python development.