Pragmatism in the real world

Using uv as your shebang line

I create a fair few scripts in my ~/bin/ directory to automate tasks. Since discovering uv and inline script metadata, I’ve started using Python far more for these.

As ~/bin is on my path, I want to run the script by calling it directly on the command line. To do this, I use this shebang:

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

The command line will now run uv run --script and pass the file as the argument. uv ignores the shebang and then runs the rest of the file as a normal Python file.

Once I’ve ensured that that script has executable permissions via chmod a+x {filname}, I’m now good to go with simple command line scripts written in Python that automatically handle their dependencies!

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *