UV
1. What is UV?
- Purpose: UV is a unified Python package manager designed to replace
pip(package installation),venv(virtual environments),pip-tools(lock files), andpipx(command-line tools). - Performance: Written in Rust, UV is faster than traditional tools.
- Maintainer: Developed by Astral, known for the Ruff linter.
- Goal: Simplify Python project management with a single tool.
2. Installation
- Methods: UV can be installed via Homebrew (easiest for Mac users), a standalone installer (
curlfor Mac/Linux, PowerShell for Windows), or other package managers. - Example: On a Mac with Homebrew, run
brew install uv. Verify installation withuvto list available commands. - Resources: Installation options are detailed on UV’s official page (linked in the video description).
3. Traditional Workflow (pip and venv)
- Steps:
- Create a project directory (
mkdir old_way). - Create a virtual environment (
python3 -m venv venv). - Activate it (
source venv/bin/activate). - Install packages (
pip install flask requests). - Create a project file (
touch main.py). - Generate a
requirements.txt(pip freeze > requirements.txt).
- Create a project directory (
- Drawbacks: Multiple steps, manual virtual environment management, and lack of intuitive dependency locking make it complex, especially for beginners.
4. UV Workflow
- Project Initialization:
- Create a new project with
uv init new_app, which sets up a directory with a Git repository,.gitignore,.python-version,README.md,main.py, andpyproject.toml. - Alternative: Run
uv initin an existing directory. - Init a project with a particular python version:
uv init . --python 3.10 - Project types:
app(default, for scripts/web servers) orlib(for distributable packages).
- Create a new project with
- Dependency Management:
- Add packages with
uv add flask requests, which updatespyproject.tomland creates auv.lockfile for reproducible environments. - UV automatically creates and manages a virtual environment (
.venv) when needed. - Visualize dependencies with
uv tree.
- Add packages with
- Running Code:
- Run scripts with
uv run main.py, which uses the project’s virtual environment without manual activation. - If the virtual environment is deleted,
uv runrecreates it usingpyproject.tomlanduv.lock.
- Run scripts with
- Environment Syncing:
- Use
uv syncto recreate an environment frompyproject.tomlanduv.lock. - Remove dependencies with
uv remove flask, which updates project files.
- Use
5. Advantages of UV
- Speed: UV’s Rust implementation and global caching system make installations faster than
pip. - Disk Efficiency: Global caching stores packages once for multiple projects, saving space.
- Reproducibility: The
uv.lockfile ensures consistent environments across machines. - Simplicity: Fewer commands and automatic virtual environment handling streamline workflows.
- Backward Compatibility: The
uv pipsubcommand mimicspipfor gradual transitions (e.g.,uv pip install numpy).
6. Migrating Existing Projects
- Steps:
- Run
uv initin the project directory. - Import dependencies from
requirements.txtwithuv add -r requirements.txt. - Delete
requirements.txtto rely onpyproject.tomlanduv.lock.
- Run
- Benefit: Converts
pip-based projects to UV’s modern workflow.
7. UV as a pipx Replacement
- Tool Installation: Install global Python tools (e.g., Ruff) with
uv tool install ruff, making them available system-wide. - Temporary Tool Usage: Run tools without permanent installation using
uv tool run ruff checkor the shortcutuvx ruff check. - Tool Management: List tools (
uv tool list), uninstall (uv tool uninstall ruff), or upgrade (uv tool upgrade --all). - Example: Install Ruff to lint code (
ruff check) or run it temporarily (uvx ruff check).
8. Additional Features
- Python Version Management: UV supports multiple Python versions via
.python-version. - Package Publishing: Commands for building and uploading packages to PyPI.
- Docker Optimization: Features for building efficient Docker containers (not covered in detail).
- Future Tutorials: The presenter plans a follow-up on Ruff and advanced UV features.
9. Why Switch to UV?
- For Beginners: Simplifies complex tasks like virtual environment setup.
- For Experts: Offers speed, reproducibility, and advanced features.
- Community Adoption: UV and Ruff are gaining popularity, with the presenter adopting UV for future videos.
- Transition Ease: The
uv pipsubcommand supports gradual adoption.
Takeaways
- Efficiency: UV consolidates multiple tools into a faster, user-friendly interface.
- Modern Workflow:
pyproject.tomlanduv.lockreplacerequirements.txtfor better dependency management. - Flexibility: Supports both new projects and migrations, plus global tool management.
- Community Impact: Backed by Astral, UV is becoming a standard in Python development.
Copied to clipboard
Share this post
Related Posts
Precedence of Logical Operators
Logical operator precedence: NOT (highest), AND (medium), OR (lowest) with Python code examples showing order.
Offline Python Packages Installation
Offline Python package installation: download dependencies with pip, transfer and install without internet.
4 Ways of Serving Static Files in Django
Four Django static file serving methods: dev server, Whitenoise, Nginx, and Amazon S3 cloud storage comparison.
LeetCode Blind 75 Golang Solutions Part I
Golang solutions to LeetCode Blind 75 Array/Hashing problems: Contains Duplicate and Valid Anagram with 100% runtime.