Setting up a project with UV
UV is a Python package manager and project manager written in Rust. It provides dependency management, Python version control, and package building capabilities. UV follows Python packaging standards (PEP 621, PEP 735) and generates reproducible dependency snapshots through lock files.
Installation
Section titled “Installation”Follow the official installation instructions for your operating system.
Standalone installer
Section titled “Standalone installer”macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | shWindows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Using pipx
Section titled “Using pipx”pipx install uvOther package managers
Section titled “Other package managers”Homebrew (macOS):
brew install uvWinGet (Windows):
winget install --id=astral-sh.uv -eVerify installation
Section titled “Verify installation”uv --versionUpdating UV
Section titled “Updating UV”If installed via the standalone installer:
uv self updateFor other installation methods, use the respective package manager’s update command.
Initializing a new project
Section titled “Initializing a new project”UV supports different project initialization modes.
Create a new application
Section titled “Create a new application”To start a new application project:
uv init my-new-projectcd my-new-projectThis creates a basic application structure:
my-new-project/├── .gitignore├── .python-version├── README.md├── pyproject.toml└── main.pyCreate a new package
Section titled “Create a new package”For a distributable package with proper structure:
uv init my-package --packagecd my-packageThis creates a package structure:
my-package/├── .gitignore├── .python-version├── README.md├── pyproject.toml└── src/ └── my_package/ ├── __init__.py └── py.typedInitialize an existing project
Section titled “Initialize an existing project”If you have an existing project, initialize UV in its root directory:
cd existing-projectuv initThis creates the necessary configuration files while preserving your existing code structure.
The pyproject.toml file
Section titled “The pyproject.toml file”UV uses the standard PEP 621 format for project metadata. Here’s an example pyproject.toml:
[project]name = "my-package"version = "0.1.0"description = "A short description of the package"authors = [ {name = "Your Name", email = "you@example.com"}]readme = "README.md"requires-python = ">=3.9"dependencies = [ "requests>=2.28.0",]
[dependency-groups]dev = [ "pytest>=7.0.0", "ruff>=0.1.0",]
[build-system]requires = ["hatchling>=1.26"]build-backend = "hatchling.build"[project]: core project metadata following PEP 621 standard.[project.dependencies]: project runtime dependencies. UV uses standard version specifiers (e.g.,>=2.28.0means version 2.28.0 or higher).[dependency-groups]: optional dependency groups (PEP 735 standard), such as development dependencies. These are not installed by default when someone installs your package.[build-system]: specifies the build backend. UV works with multiple backends includinghatchling,flit_core,setuptools, or UV’s ownuv_build.
Managing dependencies
Section titled “Managing dependencies”Adding dependencies
Section titled “Adding dependencies”To add a runtime dependency:
uv add requests# Add a specific versionuv add requests==2.28.0# Add with version constraintuv add "requests>=2.28.0"# Add as a development dependencyuv add --dev pytest# Add to a custom dependency groupuv add --group lint ruffUV automatically resolves dependencies, updates pyproject.toml, and updates the uv.lock file.
Removing dependencies
Section titled “Removing dependencies”To remove a dependency:
uv remove requests# Remove a development dependencyuv remove --dev pytestThe uv.lock file
Section titled “The uv.lock file”The uv.lock file records the exact versions of all installed dependencies (including transitive dependencies). Committing this file to version control allows all developers and CI/CD pipelines to use identical dependency versions, enabling reproducible builds.
Installing dependencies
Section titled “Installing dependencies”To install all dependencies defined in pyproject.toml (using uv.lock if it exists):
uv sync# To install only production dependencies (without dev group)uv sync --no-dev# To install a specific groupuv sync --group lint# To install all optional dependenciesuv sync --all-extras# To ensure the lockfile is up-to-date (recommended for CI)uv sync --lockedUV automatically creates and manages a virtual environment for your project in the .venv directory within your project root.
Updating dependencies
Section titled “Updating dependencies”To update dependencies to their latest compatible versions:
# Update lockfile onlyuv lock --upgrade# Update lockfile and sync environmentuv sync --upgrade# Update a specific packageuv lock --upgrade-package requests# Update and sync a specific packageuv sync --upgrade-package requestsThis will update uv.lock with the latest compatible versions according to the constraints in pyproject.toml.
Running commands
Section titled “Running commands”UV provides the uv run command to execute commands within your project’s virtual environment. The environment is synced before each run.
uv run python my_script.pyuv run pytestThis executes the specified command within the project’s virtual environment without explicitly activating a shell. UV synchronizes the environment with uv.lock before running the command. See examples in the Automated testing and Automated releases guides.
Python version management
Section titled “Python version management”UV includes built-in Python version management, eliminating the need for separate tools like pyenv.
Install a Python version
Section titled “Install a Python version”# Install the latest Python versionuv python install# Install a specific versionuv python install 3.11# Install multiple versionsuv python install 3.9 3.10 3.11 3.12List installed Python versions
Section titled “List installed Python versions”uv python listPin a Python version for your project
Section titled “Pin a Python version for your project”The .python-version file created by uv init specifies the Python version for your project. You can update it manually or use:
echo "3.11" > .python-versionBuilding and publishing packages
Section titled “Building and publishing packages”Building a package
Section titled “Building a package”To build your package for distribution:
# Build both source distribution and wheeluv build# Build wheel onlyuv build --wheel# Build source distribution onlyuv build --sdistBuilt packages are placed in the dist/ directory.
Publishing to PyPI
Section titled “Publishing to PyPI”To publish your package to PyPI:
# Publish with a token (recommended)uv publish --token $UV_PUBLISH_TOKEN# Publish to TestPyPIuv publish --publish-url https://test.pypi.org/legacy/ --token $UV_PUBLISH_TOKENYou can also use environment variables:
UV_PUBLISH_TOKEN: PyPI API tokenUV_PUBLISH_USERNAME: Username (usually__token__when using tokens)UV_PUBLISH_PASSWORD: Password or tokenUV_PUBLISH_URL: Custom index URL (e.g., for TestPyPI)
Version management
Section titled “Version management”UV includes built-in version bumping:
# Show current versionuv version# Bump patch version (1.0.0 → 1.0.1)uv version --bump patch# Bump minor version (1.0.0 → 1.1.0)uv version --bump minor# Bump major version (1.0.0 → 2.0.0)uv version --bump majorThese commands automatically update the version field in the [project] section of pyproject.toml.
Conclusion
Section titled “Conclusion”UV provides Python project setup, dependency management, and packaging capabilities. The pyproject.toml format and uv.lock file support reproducible builds across different development and deployment environments. UV includes Python version management, dependency resolution, and package building in a single tool.