PEP 722: Dependency specification for single-file scripts

Looking at the language survey posted further up, and assuming the dependencies absolutely cannot be specified in a sidecar file (I can see the */bin argument, for example[1]), can we follow the rust example of basically putting cargo.toml in a special comment?

#! /usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! leftpad-rs = "1.2.0"
//! ```

As in: make the magic comment a bit less magical by having something like

#!/usr/bin/env python
# In order to run, this script needs the following 3rd party libraries
# [regular comment line started by `#` only, special lines below started by `#!`]
#! ```toml
#! [requirements]
#! requires-python = ">=3.9"
#! dependencies = [
#!     "numpy>=1.22.4",
#!     "requests",
#! ]
#! ```
import numpy as np
import requests
print("Hello world!")

This would make it:

  • easy to extract (all lines starting with special comment #!)
  • easy to parse (after extraction it’s just a regular toml file)
  • consistent with pyproject.toml

There’s again lots of bikeshedding to be had (the special comment, how to embed the toml file, whether to leave out the main table or how to name it (e.g. just reuse [project] even though it’s only a script), etc.), but basically that would address a lot of the concerns about tooling divergence for me personally.


  1. even though I shudder to think about having scripts that go off and install something in there, especially if they happen to be executed with the “wrong” wrapper (e.g. without a virtual env). ↩︎

5 Likes