Python Leiden (NL) meetup: how to use uv for dependency management - Michiel Beijen

Tags: pun, python, django

(One of my summaries of the second Python Leiden (NL) meetup in Leiden, NL).

uv is the new python packaging solution that everybody should be using. He demoed it in a standard demo django wagtail project that still had a requirements.txt. Creating a virtualenv and doing a pip install worked, but it took a bit of time.

Next, he tried the venv/pip compatibility layer of uv. So uv venv and uv pip install -r requirements.txt. Oh… python -m venv .venv took 3 seconds, and uv venv 0.04 seconds. Wow. The uv pip was also much faster. Wow.

“Faster” is nice in development, but also when running your test in a CI enviroment (like github actions).

uv can also manage your python installations. It downloads a stand-alone python for you when needed, for instance if you need a version you don’t have locally.

Lastly, he added a pyproject.toml and put the dependencies from requirements.txt into the pyproject.toml instead. Calling uv run manage.py automatically activates the virtualenv, install everything and run manage.py just like you’d have called python manage.py.

Installing it in such a way creates an uv.lock file with all the pinned packages just such as uv downloaded them. The file should be checked in. The dependencies only say “I want whitenoise and don’t care what version”, but the uv.lock tells all your colleages (or your server..) which version you’ve installed, keeping everything in sync.

Dependency scanners also support uv.lock. At least, “renovate” already supports it and “dependabot” support is underway.

Handy: uv tool install your-tool for installing some tool like “pre-commit” in its own virtualenv somewhere and make it available globally. uvx your-tool installs the tool temporarily for just one command. And with uv add --script your-script.py some-dependency you can add dependencies to single-file scripts. Running the script with uv run your-script.py installs those dependencies for the duration of the script.

 
vanrees.org logo

Reinout van Rees

My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):