Skip to content

Support PEP-723 Style in-line metadata annotated scripts #266

@clayrosenthal

Description

@clayrosenthal

PEP-723 describes how to add comments to a script to specify what its dependencies should be similar to a pyproject.toml. The Python Packaging Docs give more information and include future updates to the spec.

example.py:

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "requests<3",
#   "rich",
# ]
# ///

import requests
from rich.pretty import pprint

resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])

uv supports running the above script directly with uv run example.py. The downside with this is that each time you run it with uv it will make calls to the network to ensure its on the correct versions of the packages each time. Fine for development, less so for production use cases.

It would be nice if shiv could do something similar like shiv --inline-script example.py --output-file example to make a portable script that needs no network requirement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions