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 addanduv removeto add and remove dependenciesuv lockanduv syncto create and synchronize with the script's lock fileuv treeto print the script's transitive dependenciesuv exportto export the script's dependencies to another format, such asrequirements.txt