ktaube.com

uv single file script

Published: at 08:15 AM

uv single file script

I’ve been using uv for a while now and I love it.

#!/usr/bin/env -S uv run
# /// script
# dependencies = [
#   "openai",
# ]
# ///

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-4o",
)

print(chat_completion)

Then you can make it executable and run it.

chmod +x script.py
./script.py