TIL: uv —script

#uv #python #til #scripting

I've known for a while that uv can run Python scripts that declare inline dependencies.

I learned today that you can also use uv to manage those dependencies.

If you have a file foo.py:

#!/usr/bin/env -S uv run

print("your code here")

you can run uv add --script foo.py numpy and it becomes:

#!/usr/bin/env -s uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "numpy",
# ]
# ///

print("your code here")

This is obviously nice if, like me, you have trouble remembering the exact syntax for declaring the inline dependencies, but it also means uv does things like dependency resolution so you don't have to manage your script's dependencies by hand.

You can add --script to: