UV Quick Reference

Installation

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Add to PATH (add to ~/.bashrc for persistence)
export PATH="$HOME/.local/bin:$PATH"

Essential Commands

Initial Setup

# Clone the repo and navigate to it
cd mlops-with-mlflow

# Sync all dependencies (creates .venv and installs everything)
uv sync

Daily Development

# Activate the virtual environment (optional, most commands work without it)
source .venv/bin/activate

# Build all packages
make build

# Run all tests
make test

# Clean build artifacts
make clean

Dependency Management

# Add a dependency to root project
uv add package-name

# Add a dev dependency
uv add --dev pytest-mock

# Add dependency to specific package
cd src/doe-library
uv add scipy

# Update all dependencies
uv sync --upgrade

# Update specific package
uv add package-name@latest

Running Scripts

# Run a Python script (ensures correct environment)
uv run python script.py

# Run pytest
uv run pytest

# Run with specific package
cd src/doe-library
uv run pytest tests/

Package Building

# Build a specific package
cd src/doe-library
uv build

# Install package in editable mode
uv pip install -e .

Common Workflows

Adding a New Package to Workspace

  1. Create package directory in src/
  2. Create pyproject.toml:
    [project]
    name = "package_name"
    version = "0.1.0"
    requires-python = ">=3.13"
    
    [build-system]
    requires = ["hatchling"]
    build-backend = "hatchling.build"
    
  3. Add to workspace in root pyproject.toml:
    [tool.uv.workspace]
    members = [
        "src/package_name",
        # ... other packages
    ]
    
  4. Run uv sync

Updating Python Version

# List available Python versions
uv python list

# Install specific version
uv python install 3.13

# Pin project to version
uv python pin 3.13

Debugging

# Check what Python is being used
uv python --version

# Check installed packages
uv pip list

# Verify package installation
uv pip show package-name

# Clean cache if having issues
uv cache clean

File Structure

mlops-with-mlflow/
├── .python-version          # Python version (3.13)
├── pyproject.toml          # Root workspace config
├── uv.lock                 # Locked dependencies
├── .venv/                  # Virtual environment (auto-generated)
├── build.sh                # Build script (uses uv)
├── test.sh                 # Test script (uses uv)
└── src/
    ├── doe-library/        # Package 1
    ├── io-library/         # Package 2
    └── ...

Environment Variables

# Prefer copying over hardlinking (useful for cross-filesystem scenarios)
export UV_LINK_MODE=copy

# Use specific Python version
export UV_PYTHON=3.13

# Custom cache location
export UV_CACHE_DIR=/path/to/cache

Troubleshooting

ProblemSolution
uv: command not foundAdd $HOME/.local/bin to PATH
Python 3.13 not foundRun uv python install 3.13
Sync failsTry uv cache clean && uv sync
Package not foundCheck it’s in workspace members
Build failsEnsure requires-python = ">=3.13"

vs pip/venv

Taskpip/venvuv
Create venvpython -m venv .venvuv sync
Activatesource .venv/bin/activateNot needed for uv run
Install depspip install -r requirements.txtuv sync
Add packagepip install packageuv add package
Run scriptpython script.pyuv run python script.py
Lock depspip freeze > requirements.txtuv lock (automatic)

Tips

  1. No activation needed: uv run automatically uses the right environment
  2. Lock file: uv.lock is auto-generated, commit it for reproducibility
  3. Workspace: All packages share the same environment, faster installs
  4. Speed: ~50x faster than pip for most operations
  5. Standards: Uses PEP 621 and modern packaging standards