skip to content

Migrating python projects to uv

I finally had the chance to test Astral's uv tool. It's very good, from what I've seen so far.

I first tested it on a simple cli app I am building with click. The changes were minimal as I was already using a pyproject.toml file for the project.

Setup the virtual environment

I nuked my current venv, added a .python-version file to the project root, and used uv to create a new venv

❯ rm -rf venv
echo '3.13' > .python-version
❯ uv venv

Add dependencies

To move dependencies from requirements.txt to pyproject.toml I just used the REPL

❯ python
Python 3.13.0 (main, Oct 16 2024, 08:05:40) [Clang 18.1.8 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> import subprocess
>>> with open("requirements.txt", "r") as f:
...      for line in f.readlines():
...         if len(line) < 2 or line[0] == "#":
...             continue
...         package = re.sub(r"[\s=].*$", "", line, flags=re.IGNORECASE)
...         subprocess.run(f"uv add {package}", shell=True)

That worked because my project setup was quite simple.

Add the build system

The project already had a [project.scripts] section with the entry point for the CLI app. All I had to do was to add this section to the pyproject.toml


[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

What I liked so far

UV is supposed to be fast, but my projects are so small I don’t notice the difference to be honest. I like not having to use pyenv and pip and venv. I like that uv picks up the .python-version seamlessly, and not having to do poetry env use X. I like that I can create util scripts which have their own dependencies and run in a standalone virtual env.

It’s not a game changer for me (yet), but it seems very nice indeed