Running Python scripts with Just and uv

#just#uv#python

Use Just task runner with uv’s inline script support to run small Python scripts without managing venvs.

By using uv’s inline script metadata and a shebang executable in a Justfile, you can define Python scripts with automatic venvs as outlined in “TIL: Running Python scripts with uv shebang”:

get-url url:
    #!/usr/bin/env -S uv --quiet run --script
    # /// script
    # requires-python = ">=3.11"
    # dependencies=["requests", "typer", "beautifulsoup4"]
    # ///
    import requests
    from bs4 import BeautifulSoup

    response = requests.get("{{url}}")
    soup = BeautifulSoup(response.text, 'html.parser')
    title = soup.title.string if soup.title else "No title found"
    print(f"Page Title: {title}")

Run it with: just get-url https://example.com

Just recognises the shebang #!/usr/bin/env -S uv --quiet run --script and writes the recipe to a temporary file and then passes it to execute with uv, which creates an isolated environment based on the metadata which Just creates.

Just passes the url parameter into the script using {{url}} template syntax.

The # /// script block defines Python version requirements and dependencies. uv automatically creates an isolated venv and installs dependencies on first run - reusing it on subsequent executions.

Top