Contents

PEP-723 is great!

Contents

PEP 723 standardises new inline metadata for scripts. It’s most useful when writing stand-alone scripts as the dependencies can be managed within the script itself without having to setup a complete project and create pyproject.toml file.

Example

Here’s a quick example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env -S uv run --no-cache

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "nltk",
#   "rich",
# ]
# ///

import nltk
from rich import print

def main():
    sentence = "Good morning!  How are you?"
    nltk.download('punkt_tab', quiet=True)
    tokens = nltk.word_tokenize(sentence)
    print(tokens)

if __name__ == "__main__":
    main()

As you might have noticed, I’m using uv as part of shebang so, it’s possible to just run the script on its own and it will magically take care of its own dependencies (which basically means that uv will take care of creating a dedicated virtual environment in which it will install all required dependencies prior to running the script):

In this example, I’m using --no-cache in the shebang but, most likely, you’ll want to drop that.

Syntax

The format is as follows:

1
2
3
4
# /// TYPE
#
# ...
# ///

At the moment, there’s only script1 type defined with two fields: requires-python and dependencies.

PEP-723 aware tool is needed as well. I’m using uv which introduced support in version 0.2.0 (April 2024).