Ever wanted to share a Python script that uses external packages without making the recipient set up a virtual environment? With uv, you can embed dependencies directly in the script.

The Command

uv add --script example.py 'requests<3' 'rich'

This adds inline metadata to your script:

# /// script
# 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])

Running It

Anyone with uv installed can now run the script directly:

uv run example.py

uv reads the embedded metadata, installs dependencies in an isolated environment, and executes the script. No requirements.txt, no venv, no friction.

Why This Matters

This is perfect for:

  • Sharing utility scripts with teammates
  • Quick prototypes that need packages
  • Scripts you want to version-control as single files

Reference