Python Inline Script Metadata Format

scripting, python

Python Scripts with Inline Metadata: a game-changer for quick dependency management

Ever wished you could define dependencies right inside your quick pythons scripts without wrestling with complex configuration files? Hey presto, Python's got a slick feature that gets you one step closer to that 10x engineer.

What's the Magic?

Python now supports inline metadata that tools like uv can read and manage in virtual environments. It's modern, scalable, and ridiculously simple to use.

The Inline Metadata Format

Check out this super-clean example that defines Python version requirements and dependencies metadata right in the script file itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# /// script
# requires-python = "<3.11"
# dependencies = [
#   "requests<3.0.2",
# ]
# ///

import platform
import sys
import requests


def get_cute_doggo():
    res = requests.get("https://dog.ceo/api/breed/frise/bichon/images/random")
    res.raise_for_status()
    return res.json().get("message")

print(get_cute_doggo())
print(sys.version_info)
Note: This isn't just syntactic sugar - it's a powerful way to make your scripts totally self-contained!

How It Works in Action

Here's what happens when you run the script with uv:

1
2
3
4
5
6
[25-01-24 08:12:56] toby@aws_tmp_aunx17s/:~/dev 
λ: uv run ./my_script.py
Reading inline script metadata from `my_script.py`
{"message":"https:\/\/images.dog.ceo\/breeds\/frise-bichon\/5.jpg","status":"success"}
https://images.dog.ceo/breeds/frise-bichon/5.jpg
sys.version_info(major=3, minor=10, micro=15, releaselevel='final', serial=0)

Breaking it Down:

  • requires-python = ">=3.11" ensures the script runs on Python 3.10 or earlier.
  • dependencies list specifies exactly which packages (and versions) you need
  • uv automatically creates a virtual environment with these exact requirements

Why This Rocks

Imagine never again dealing with separate requirements.txt files or complex pyproject.toml configurations for simple things. With inline metadata, your dependencies travel right alongside your code - clean, simple, and straightforward.

Try changing the example dependencies or versions, see what happens!

Hey presto! You're now equipped with one of Python's coolest features. Go forth and simplify your dev_scripts folder! 🐍✨