TIL: uv —script
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:
uv add
anduv remove
to add and remove dependenciesuv lock
anduv sync
to create and synchronize with the script's lock fileuv tree
to print the script's transitive dependenciesuv export
to export the script's dependencies to another format, such asrequirements.txt