UV Python Package Manager Guide
Resources
What is UV?
UV is a fast Python package installer and resolver developed by Astral, written in Rust. Designed as a modern alternative to pip and pip-tools, it offers significantly faster dependency resolution and package installation. UV is fully compatible with pip, allowing it to seamlessly replace existing pip commands while delivering substantial performance improvements.
Why Use UV?
- Ultra-fast Installation: 10-100x faster than traditional pip
- Parallel Downloads: Simultaneously downloads multiple packages to save time
- Smart Caching: Efficiently caches downloaded packages
- Compatibility: Fully compatible with existing pip commands and workflows
- Better Dependency Resolution: Avoids dependency conflicts for more reliable environments
Installing UV
Installing UV is straightforward:
1 | |
Basic Usage
0. Simple Mode
Installing Packages
Single package installation:
1 | |
Installing from requirements.txt:
1 | |
Installing specific versions:
1 | |
Uninstalling Packages
1 | |
Listing Installed Packages
1 | |
Upgrading Packages
1 | |
Dependency Management
Generating requirements.txt:
1 | |
Virtual Environment Management
UV provides powerful virtual environment management capabilities, making it easy to create and manage environments with different Python versions.
Creating Virtual Environments
Creating a default environment:
1 | |
Specifying a Python version:
1 | |
Using a specific Python interpreter from your system:
1 | |
Activating Virtual Environments
On Linux/macOS:
1 | |
On Windows:
1 | |
Installing Packages in Virtual Environments
Creating and installing dependencies in an environment:
1 | |
Project Mode
1 | |
Advanced UV Features
Using pyproject.toml
UV supports installing development dependencies from pyproject.toml:
1 | |
1 | |
Syncing Dependencies
Ensuring your environment exactly matches requirements.txt:
1 | |
Accelerating with Parallel Installation
UV uses parallel installation by default, but you can adjust the parallelism:
1 | |
Repository Mirror Configuration
Using alternative package repositories for a single installation:
1 | |
Global Mirror Configuration
To set up global mirror configuration for UV, you can use environment variables or configuration files:
Method 1: Environment Variables
Set the default index URL globally:
1 | |
For permanent configuration, add this to your shell profile (.bashrc, .zshrc, etc.):
1 | |
Method 2: Configuration File
Create a UV configuration file at ~/.config/uv/uv.toml (Linux/macOS) or %APPDATA%\uv\uv.toml (Windows):
1 | |
Method 3: Project-specific Configuration
For project-specific mirror settings, add to your pyproject.toml:
1 | |
Common Mirror URLs:
- Tsinghua University:
https://pypi.tuna.tsinghua.edu.cn/simple - Alibaba Cloud:
https://mirrors.aliyun.com/pypi/simple - Douban:
https://pypi.douban.com/simple - USTC:
https://pypi.mirrors.ustc.edu.cn/simple
Performance Optimization Tips
- Leverage UV’s caching to reduce repeated downloads
- Use
uv pip compileto generate lock files for large projects - Utilize the
--jobsparameter to adjust parallelism for your system’s capabilities
Comparison with Other Tools
| Feature | UV | pip | pip-tools | Poetry |
|---|---|---|---|---|
| Installation Speed | Extremely Fast | Slow | Moderate | Moderate |
| Dependency Resolution | Precise | Basic | Good | Good |
| Virtual Environment Management | Supported | Not Supported | Not Supported | Supported |
| Compatibility | High | High | High | Moderate |
| Parallel Downloads | On by Default | Not Supported | Not Supported | Supported |
Troubleshooting Common Issues
UV Installation Failures
If you encounter issues installing UV, try:
1 | |
Dependency Resolution Conflicts
When facing dependency resolution conflicts:
1 | |
Using the --strict parameter provides more detailed conflict information.