UV Tutorial Part 1: Lightning-Fast Old-Style Python Project Management With UV

Introduction

Hey there! If you’re used to managing your Python projects with tools like pip, pyenv, virtualenv, or conda, I’ve got some exciting news for you. There’s a tool called uv (Universal Virtualenv) that’s going to revolutionize your workflow. It’s unbelievably fast – we’re talking 10-100x faster than pip. Let’s dive into how you can get started with uv and make your project management a breeze.

Installation Guide

First things first, let’s get uv installed on your system.

Windows Installation

If you’re on Windows, open PowerShell and run:

1
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

Linux/Mac Installation

For those using Linux or Mac, you can install uv with this command:

1
curl -LsSf https://astral.sh/uv/install.sh | sh

Installing via pip

Alternatively, you can install uv using pip:

1
pip install uv

Keeping UV Updated

To keep uv up to date, simply run:

1
uv self update

Python Version Management

One of the coolest features of uv is its ability to manage different Python versions effortlessly. To see the list of available Python versions, use:

1
uv python list

Traditional Python Project Setup vs UV Approach

Let’s compare the traditional way of setting up a Python project with the uv approach.

Traditional Method with virtualenv

Here’s how you might traditionally set up a new Python project using pip and virtualenv:

  1. Create a new directory
  2. Create a virtual environment: virtualenv venv
  3. Activate the virtual environment:
    • Linux: source .venv/bin/activate
    • Windows: .\venv\Scripts\activate
  4. Install packages: pip install requests
  5. Generate requirements: pip freeze > requirements.txt

Project Migration (Traditional)

When you want to move your project to another machine, you typically:

  1. Copy the project directory
  2. Create a new virtual environment
  3. Install dependencies: pip install -r requirements.txt

Modern Approach with UV

Now, let’s see how uv can simplify this process and make it much faster:

  1. Create a new directory
  2. Create a virtual environment: uv venv or uv venv --python 3.10 for a specific Python version
  3. Activate the virtual environment:
    • Linux: source .venv/bin/activate
    • Windows: .\venv\Scripts\activate
  4. Install packages: uv pip install requests
  5. Generate requirements: uv pip freeze > requirements.txt

Project Migration with UV

Moving your project to another machine with uv is a breeze:

  1. Copy the project directory
  2. Create a virtual environment: uv venv or uv venv --python 3.10
  3. Install dependencies: uv pip sync requirements.txt

Converting Existing virtualenv to UV

Already using a traditional virtualenv? No problem! Converting it to uv is super simple:

  1. Navigate to your project directory with the existing virtual environment.
  2. Run: uv init to adopt the existing environment.
  3. Convert dependencies: uv add -r requirements.txt to create pyproject.toml and uv.lock files.

Key Benefits of UV

To sum it up, here are some key benefits of using uv:

  • Significantly faster package installation
  • Better dependency management
  • Easy Python version control
  • Seamless conversion from existing virtualenv setups

What’s Next?

Stay tuned for Part 2, where we’ll explore how to create new projects with uv, add packages, and manage your project like a pro. In part 3, we will talk installing PyTorch with CUDA.


Related Content

0%