Quick Note 2024-09-15

Experience using UV

What is UV?

UV is a Python package and project manager created by Astral, the same company that created ruff and has a focus on high performance tooling.

Package Manager Capabilities

Can resolve and install packages similar to pip, as well as create reproducible environments with lockfiles similar to pip-tools.

Has a nice UX for adding and removing packages that is similar to npm or yarn. You can run uv add <dependency>, and that will resolve the dependency suitable to your current environment, add it in your constraints file i.e. pyproject.toml, as well as add it to the lockfile (uv.lock). Then when you go ahead and run something in your project, such as an entry point via uv run <entry point>, uv will ensure that your project environment is synced before running the file.

Here is an example of what this would look like in pip and pip-tools:

$ <edit constraint file to add dependency>
$ pip-compile pyproject.toml -o requirement.txt
$ pip-compile pyproject.toml -c requirement.txt -o requirement-dev.txt
$ pip-sync requirement-dev.txt
$ .../bin/<entry point>

Similarly in uv it is just two lines:

$ uv add <dependency>
$ uv run <entry point>

Project Manager Capabilities

Able to manage the tool chain for the projects including the Python versions. Normally this would be controlled by another tool like pyenv to bootstrap the virtual environment required, now instead this can be done via uv init <project name>.

Has the concept of ephemeral tools, so you can run a command from a tool in your project without installing it in your projects environment via uvx <command>. You can also specify the ephemeral environment to come with dependencies not specified by the tool itself via uvx --with <dependency> <command>. This came in handy with a copier template I was using that required a copier_templates_extensions package installed along with copier to access a jinja2 template that the template author required.

Installing a tool into your project environment is also as simple as uv tool install <package>, where this gets adding to your project dependencies and lockfile.

You can build your project into a source distribution or wheel with uv build, and it has other useful commands built-in such as uv tree to show your project dependencies in a tree like structure.

Missing Features

A feature included in pdm that would be very useful to have is the concept of running and name spacing arbitrary scripts. This is being tracked in UV-5903, and seems like there is a lot of promise that this will be included but potentially under the concept of tasks instead of scripts to avoid name collision with single script deployments.