How to get started with uv

Package management under Python can be a bit of a mess, one of the complaints I had about Python in the past. It’s not uncommon for different programs to not only require different packages, but also completely different Python versions. Python itself has virtual environments to allow different package trees for different programs, but this may not be sufficient. Conda is an alternative that can install different Python versions for different environments, and is the de-facto standard for many machine learning applications, but it is not perfect. I see it often being used with pip instead of conda’s own package manager, which takes away some of the benefit of conda, but then the conda package manager can be extremely slow.

More recently, uv has appeared on the scene as alternative. It’s written in Rust and uses a common package cache where possible instead of separate caches for each environment. Since the system Python on macOS is not recommended for actual development use, I decided to give uv a try here.

Installation

The installation is straightforward under all major operating systems and explained here. I used the standalone installer. You can also install it using Homebrew under macOS or Winget under Windows, and there’s even the option to use a Docker image.

You then need to install a Python version, which is done with uv python install. It’s possible to install specific Python versions:

uv python install 3.14

Running scripts

Running python scripts is done with uv run:

uv run scriptname.py

Working with projects

The main way to use uv us by working in projects. This is quite similar to how Rust works – not surprising given that uv is written in Rust.

A new project is created with uv init. You can either call it with a name as argument, which will create a new directory, or call it inside an empty directory. Either way, it will scaffold a project including a git repository and a main.py file. There’s also a pyproject.toml file that contains metadata and dependencies, just like a cargo.toml file in a Rust project.

Packages are then added with uv add packagename. These are added to the pyproject.toml file and installed. In a machine learning project using torch, you would install the dependencies with

uv add torch torchvision

Each project directory is a self-contained virtual environment. So there’s no longer the need to manually activate environments – you simply enter a directory and let uv run do the rest.

You can then even use uv build to build source and binary packages of your project, making distribution quite easy.

Conclusions

I was hesitant at first, as I was so used to the normal pip workflow and using conda under Linux for managing virtual environments. But as I’m a big fan of cargo, it made sense to give uv a try. I really like that projects are synonymous with virtual environments, and that the enabling of these is done automatically.

 

Een reactie plaatsen

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *