Learn how to use UV, the fast Python package manager.
UV is a modern Python package manager written in Rust that’s designed to be fast and reliable.
Speed ⚡
Reliability 🔒
Simplicity 🎯
pyproject.tomlpip, venv, pyenv separately| Task | Traditional | UV |
|---|---|---|
| Create venv | python -m venv .venv |
Automatic |
| Activate venv | source .venv/bin/activate |
Not needed |
| Install deps | pip install -r requirements.txt |
uv sync |
| Add package | pip install package && pip freeze |
uv add package |
| Run script | python script.py |
uv run python script.py |
uv init my-project
cd my-project
This creates:
pyproject.toml - Project configuration.python-version - Python version specificationREADME.md - Project readmeuv sync
What it does:
pyproject.toml.venv/ virtual environmentuv.lockWhen to use:
.venv/ folderuv add package-name
Examples:
uv add numpy # Add latest version
uv add numpy >=2.0.0 # Add with version constraint
uv add numpy --dev # Add as dev dependency
What happens:
pyproject.toml is updated automaticallyuv.lock is updateduv remove package-name
Example:
uv remove numpy
What happens:
pyproject.tomluv.lock is updateduv run python script.py
Why use uv run?
Examples:
uv run python analysis.py
uv run python -c "import polars; print(polars.__version__)"
uv run marimo edit notebook.py
pyproject.tomlThe pyproject.toml file is the heart of your project configuration.
[project]
name = "my-project"
version = "0.1.0"
description = "My awesome project"
requires-python = ">=3.12"
dependencies = [
"marimo>=0.9.0",
"polars>=0.20.0",
"plotly>=5.18.0",
]
[project] - Project metadata
name: Your project name (lowercase, hyphens)version: Current version (semantic versioning)description: Short descriptionrequires-python: Minimum Python versiondependencies - Production packages
"package>=version">=, <=, ==, ~=[project.optional-dependencies] - Optional packages
dev: Development tools (linting, testing)docs: Documentation toolsdependencies = [
"polars>=0.20.0", # At least 0.20.0
"plotly~=5.18.0", # Compatible with 5.18.x
"requests==2.31.0", # Exactly 2.31.0
"numpy>=1.24,<2.0", # Between 1.24 and 2.0
]
Recommendations:
>= for most packages (get bug fixes)~= when you need stability== only when absolutely necessaryUV can manage Python installations for you!
uv python list
uv python install 3.12
uv python install 3.11
Edit pyproject.toml:
[project]
requires-python = ">=3.12"
Or create .python-version:
3.12
UV will automatically use this version when you run uv sync or uv run.
UV manages virtual environments automatically, but here’s what’s happening:
When you run uv sync, UV creates .venv/ in your project directory.
IntroDataScience/
├── .venv/ # Virtual environment (auto-created)
│ ├── bin/ # Executables (python, pip, marimo)
│ ├── lib/ # Installed packages
│ └── pyvenv.cfg # Configuration
├── pyproject.toml # Your configuration
└── uv.lock # Locked versions (auto-generated)
Usually not needed, but available:
# Create new venv
uv venv
# Create with specific Python version
uv venv --python 3.12
# Create in custom location
uv venv /path/to/venv
uv init my-data-project
cd my-data-project
uv add polars plotly marimo
uv run marimo edit analysis.py
# Need seaborn for visualization
uv add seaborn
# Use it immediately
uv run python
>>> import seaborn
# Update all packages to latest compatible versions
uv sync --upgrade
# Update specific package
uv add package@latest
# List all installed packages
uv pip list
# Show dependency tree
uv pip show package-name
# Check for outdated packages
uv pip list --outdated
To share:
pyproject.toml to gituv.lock to git (ensures same versions).venv/ (add to .gitignore)For others to use:
git clone your-repo
cd your-repo
uv sync # That's it!
Some tools still need requirements.txt:
uv pip freeze > requirements.txt
| pip Command | UV Equivalent | Notes |
|---|---|---|
pip install package |
uv add package |
Also updates pyproject.toml |
pip install -r requirements.txt |
uv sync |
Uses pyproject.toml |
pip list |
uv pip list |
Same output |
pip freeze |
uv pip freeze |
Same output |
python -m venv .venv |
uv sync |
Automatic |
source .venv/bin/activate |
Use uv run |
No activation needed |
| conda Command | UV Equivalent | Notes |
|---|---|---|
conda create -n env |
uv sync |
Automatic |
conda activate env |
Use uv run |
No activation needed |
conda install package |
uv add package |
|
conda env export |
pyproject.toml |
Version controlled |
Key Difference:
UV caches everything:
# Build cache
uv sync
# Later, work offline
uv sync --offline
# Clear download cache
uv cache clean
# See cache location
uv cache dir
[[tool.uv.index]]
url = "https://your-company-pypi.com/simple"
uv.lock contains exact versions of all dependencies:
uv sync --upgrade# Project setup
uv init project-name # New project
uv sync # Install dependencies
# Package management
uv add package-name # Add package
uv remove package-name # Remove package
uv add package --dev # Add dev dependency
# Running code
uv run python script.py # Run script
uv run marimo edit nb.py # Run marimo notebook
# Information
uv pip list # List installed packages
uv python list # List available Python versions
uv --version # Check UV version
# Maintenance
uv sync --upgrade # Update all packages
uv cache clean # Clear cache
# General help
uv --help
# Command-specific help
uv add --help
uv sync --help
uv run --help
Official Documentation:
UV makes Python package management simple and fast. Once you try it, you won’t want to go back! 🚀