Python developers juggle multiple tools like pip, venv, and pipx for dependency management, virtual environments, and tool installations. Enter UV, a modern, Rust-based tool designed to replace these utilities with a single, blazing-fast interface. Developed by Astral (the team behind the popular linter Ruff), UV streamlines workflows while offering significant performance gains. In this guide, you’ll learn how to harness UV to simplify and accelerate your Python projects.
Why UV?
-
Speed: Written in Rust, UV installs packages and creates environments up to 100x faster than traditional tools.
-
Unified Workflow: Combines
pip,venv,pip-tools, andpipxinto one tool. -
Smart Caching: Reduces disk space by reusing packages across projects.
-
Reproducibility: Generates lock files (
uv.lock) for consistent environments. -
Tool Management: Install and run CLI tools globally or temporarily with
uvx.
Installation
UV supports macOS, Linux, and Windows. Choose your preferred method:
Homebrew (macOS/Linux):
brew install uv
Standalone Installer:
curl -LsSf https://astral.sh/uv/install.sh | sh
Windows (PowerShell):
irm https://astral.sh/uv/install.ps1 | iex
Verify installation by running uv --help.
Getting Started with UV
1. Create a New Project
Initialize a project with a modern pyproject.toml structure:
uv init new-app && cd new-app
This generates:
-
pyproject.toml: Configures dependencies and project metadata. -
uv.lock: Lock file for exact dependency versions. -
.gitignoreandREADME.md: Standard project files.
2. Add Dependencies
Install packages like Flask and Requests:
uv add flask requests
UV automatically:
-
Creates a virtual environment (
.venv). -
Updates
pyproject.tomlanduv.lock. -
Uses global caching to speed up installations.
3. Run Your Code
Execute scripts using the project’s environment without manual activation:
uv run main.py
If the virtual environment is missing, UV recreates it on the fly.
4. Sync Dependencies
To replicate environments (e.g., on another machine):
uv sync
This rebuilds the environment using pyproject.toml and uv.lock.
Key Commands
| Command | Description |
|---|---|
uv add <package> |
Install packages and update configs. |
uv remove <package> |
Remove packages and update configs. |
uv sync |
Recreate the environment from lock files. |
uv run <script> |
Execute code in the project’s environment. |
uv tool install rough |
Install CLI tools globally (like pipx). |
uvx rough check . |
Run a tool temporarily without installation. |
Migrating Existing Projects to UV
-
Initialize UV in your project directory:
uv init -
Import dependencies from
requirements.txt:uv add -r requirements.txt -
Delete
requirements.txtand rely onpyproject.tomlanduv.lock.
Advanced Features
-
Global Tool Management: Install tools like linters or formatters:
uv tool install black -
Temporary Tools: Run one-off commands without installation:
uvx cowsay "Hello, UV!" -
Python Version Management: Specify versions in
pyproject.tomlfor consistency.
Why UV Wins Over Traditional Tools
-
No More Manual Virtual Environments: UV handles environment creation silently.
-
Faster Pipelines: CI/CD workflows benefit from UV’s speed.
-
Space Efficiency: Shared caching reduces redundant package storage.
-
Beginner-Friendly: Simplifies workflows by abstracting complexity.
Conclusion
UV is reshaping Python development by merging essential tools into a single, efficient interface. Whether you’re tired of slow installations or managing multiple tools, UV offers a modern solution.
Next Steps:
-
Explore UV’s documentation for advanced features.
-
Try Ruff, Astral’s Python linter, for faster code checks (
uv tool install ruff).
By adopting UV, you’ll spend less time configuring environments and more time building. Give it a try—your workflow will thank you!
📄 Copyright: Unless otherwise noted, all articles are original. Please cite the source when sharing.