TIL

uv based scripts with dependencies

23.02.2025

I came over a blogpost Lazy self-installing Python scripts with uv which described how you can use uv to declare dependencies for a script with a shebang. Thus, you can for instance use typer to create some scripts with validation and --help functionality. Like this example:

#!/usr/bin/env -S uv run --script
# /// script
# dependencies = ["typer"]
# ///
import typer
def main(name: str):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)

This is pretty neat for those scripts you need something more than shell or even just python standard lib. One use case that comes to mind is xbar scripts. The linked blogpost has more details on how it works if you are interested.