When using python scripts, traditionally is required beforehand to create a virtual environment and install dependencies or requirements. However, with the help of uv we can run scripts directly without the need for a virtual environment.
Problem #
Simple method for running a python scripts similar to executing binaries. In other words:
1script --help
Solution #
Create script file #
1#!/usr/bin/env -S uv run --script
2# /// script
3# dependencies = ["typer"]
4# ///
5
6import typer
7
8def main() -> None:
9 typer.secho("Hello!", fg=typer.colors.MAGENTA)
10
11if __name__ == "__main__":
12 typer.run(main)
13
Make script executable #
1chmod +x script.py
2mv script.py ~/.local/bin/script
Explanation #
The shebang line !/usr/bin/env -S uv run --script uses uv to run the script directly without the need for a virtual environment.
The special # /// script tags are used to specify inline script metadata, like dependencies.
Lastly, the script is made executable and moved to the user's local bin directory, so it can be run as a command.
last updated: