Browse the docs

Run Python script

Runs Python 3 source and captures what it prints.

Run Python script

Details #

Sends script to python3 - (found with /usr/bin/env) on standard input. What the program prints to standard output is the step's output, with trailing newlines removed, and goes into into if set. The output object has stdout, exit and, if any, stderr. An exception or non-zero exit fails the step with the first 300 characters of standard error.

To pass structured data out, print(json.dumps(...)) and read the text in a later step.

Parameters #

ParameterTypeRequiredValuesWhat it does
scripttextYesRequired. Python 3 source.
intotextThe variable that receives the printed text.
sampleany valueWhat into holds during a dry run.
timeoutnumberSeconds before the step is cancelled.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Add up numbers in Python (runs Python)
Do
  1. Run Python script “print(sum(range(1, 11)))”
  2. Show “Total: {{total}}”
Open in Switchboard to try it

Gotchas #

  • Needs Python 3. It uses whichever python3 the environment finds first. If none is installed the step fails; install Python or the Xcode command line tools.

  • Dry run. Not run: into gets sample or an empty string.

  • Timeout. Cancelled after 10 s by default; set timeout for longer jobs.

  • {{ }} values are pasted into the source before it runs, so quote untrusted text carefully. Do not store secrets in the script text.

See also #

Run shell script, Run JavaScript