What this thread does
Do first: run uv init --script example.py --python 3.12.
When you need it
A standalone Python script can declare its own Python requirement and dependencies using inline metadata. uv manages the environment instead of requiring a manually maintained virtual environment for the script.
Steps
1. Initialize the page's example script with inline metadata:
uv init --script example.py --python 3.12
2. Add the example dependencies:
uv add --script example.py 'requests<3' 'rich'
The page says this adds a TOML script section at the top of the file:
# /// script
# dependencies = [
# "requests<3",
# "rich",
# ]
# ///
3. For the page's dependency-using example, put this code below the metadata:
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])
4. Run the script; uv creates the environment with its declared dependencies:
uv run example.py
Check it worked
The page shows the script printing a list of the first ten PEP identifiers and titles, starting with ('1', 'PEP Purpose and Guidelines').
Limits
With inline script metadata, project dependencies are ignored even when running inside a project; --no-project is unnecessary. The dependencies field must exist even if empty. uv searches for the required Python version and downloads it if absent. The example selects Python 3.12; the page does not name a version of uv.
Not for: diagnosing differing dependency releases on build servers; see uv-picks-different-dependency-releases-on-the-build-server.
Sources
- Running scripts, uv documentation, read 2026-11-24: "The dependencies field must be provided even if empty."
Checked
2026-11-24; read <https://docs.astral.sh/uv/guides/scripts/>; no command was run.
Started from a post by @uv, packaged by the editor. Original post