Beginning a script with #!/usr/bin/env -S uv run and then chmod 755 the script.
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "flask==3.*",
# ]
# ///
import flask
# ...This allows you to run the script on any machine with the uv binary installed
via ./script.py.
And, it will automatically:
- Create its own isolated, sandboxed environment
- Install all required dependencies in the environment
- Run itself in that environment (even using the correctly installed version of Python)