References:
uv is an extremely fast, Rust-based Python package installer and project manager. It unifies the functionality of multiple tools (pip, pip-tools, pipx, poetry, pyenv, virtualenv) into a single, cohesive binary.
Key Benefits:
- Speed: 10-100x faster than pip.
- Unified Workflow: One tool for projects, tools, and python versions.
- Global Caching: Saves disk space by sharing packages across environments (via hard links) while keeping environments isolated.
- Reproducibility: Automatically manages
uv.lockfor deterministic builds.
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"curl -LsSf https://astral.sh/uv/install.sh | shAlso available via Homebrew: brew install uv
Best for new projects or modern workflows using pyproject.toml.
Sets up project structure (.python-version, pyproject.toml, .gitignore, hello.py).
# Initialize in current directory
uv init
# Initialize a library/application specifically
uv init --app my-app
uv init --lib my-lib
# Initialize with a specific Python version
uv init --python 3.9During Init:
Pass the --python argument as shown above.
Existing Project:
To change the Python version for a project already managed by uv:
- Run
uv python pinto update the.python-versionfile. - Run
uv syncto update the virtual environment and lockfile.
# Switch project to Python 3.11
uv python pin 3.11
uv syncuv automatically creates/manages the virtual environment (.venv) and lockfile (uv.lock) when you modify dependencies.
# Add a dependency (updates pyproject.toml & uv.lock, installs into .venv)
uv add pandas
# Add a development dependency (e.g., testing, linting)
uv add --dev pytest ruff
# Remove a dependency
uv remove pandasRun scripts or commands within the project's environment context. uv ensures the environment is up-to-date before running.
# Run the main script
uv run hello.py
# Run a command (e.g., calling a library)
uv run python -m http.server# View dependency tree
uv tree
# Sync environment with lockfile (install/uninstall to match exact state)
uv syncManage globally installed Python CLIs (like ruff, black, yt-dlp) without polluting your system Python.
Download and run a tool temporarily without installing it permanently.
# Run ruff check on current dir
uvx ruff check .
# OR
uv tool run ruff check .# Install a tool
uv tool install ruff
# List installed tools
uv tool list
# Upgrade all tools
uv tool upgrade --all
# Uninstall a tool
uv tool uninstall ruffFor simple scripts or when you need manual control over virtual environments.
# Create a venv (defaults to .venv)
uv venv
# Create with specific Python version
uv venv --python 3.10
# Activate
# Windows: .venv\Scripts\activate
# Mac/Linux: source .venv/bin/activateuv pip commands are drop-in replacements for pip, but faster.
# Install a package
uv pip install numpy
# Install from requirements.txt
uv pip install -r requirements.txt
# Compile requirements (pip-compile replacement)
uv pip compile pyproject.toml -o requirements.txtuv can download and manage Python executables itself.
# Install a specific Python version
uv python install 3.12
# List available versions
uv python list
# Pin project to a specific version
uv python pin 3.11Generate a requirements.txt file for other tools or CI/CD pipelines.
Includes all dependencies (including dev dependencies) and hashes for security.
uv export --format requirements-txt > requirements.txtExclude development dependencies (e.g., pytest, ruff) for production builds.
uv export --no-dev --format requirements-txt > requirements.txtBy default, uv export includes hashes for strict pinning. To generate a cleaner list without hashes:
uv export --no-hashes --format requirements-txt > requirements.txtuv export uses the uv.lock file as the source of truth, so the output requirements.txt will always contain exact pinned versions of all dependencies (transitive included). This ensures that the environment is reproducible.
| Goal | Old Way | UV Way |
|---|---|---|
| Create Project | mkdir + touch |
uv init |
| Create Venv | python -m venv .venv |
uv venv (or handled auto by uv run) |
| Install Pkg | pip install pkg |
uv add pkg (project) or uv pip install pkg (script) |
| Run Script | source .venv/bin/activate && python script.py |
uv run script.py |
| Lock Deps | pip freeze > requirements.txt |
uv.lock (automatic) |
| Run Tool | pipx run tool |
uvx tool |