Execute Python scripts

The Run Python activity allows you to execute Python scripts from server workflows. This page explains how to structure your script so it runs successfully, and how to troubleshoot some common problems.

Requirements

The script that you specify via the Python File input must meet certain requirements to execute successfully.

The file path must:

The file contents must:

Examples

Example 1: Does not use the parameters, but always returns the same string.

Copy
def main(args):
    return "Hello there"

Example 2: Returns the first value supplied via the Arguments input. Note we use args[1] here because args[0] is the file path of the script itself.

Copy
def main(args):
    return args[1]

Example 3: Converts parameters to numbers and uses a separate function to add them. The result is returned in a sentence.

Copy
def sum(a, b):
    return (a + b)

def main(args):
    val1 = int(args[1])
    val2 = int(args[2])
    return f'The sum of {val1} and {val2} is {sum(val1, val2)}.'

Example 4: Exits the script indicating an error. You can use whatever exit code you want, but an exit code other than zero will cause the success output to be false.

Copy
import sys

def main(args):
    sys.stderr.write("Example error in script.")
    sys.exit(-999)

Troubleshooting

The success output is only ever true if the exitCode is 0. Otherwise, you can refer to the error and exitCode outputs to learn more about what went wrong.

Here are some exit codes that you might encounter, along with examples of the error message and an explanation of the cause.

Exit Code

Example Error Message

Cause

-1 Incorrect filename format: Filename: C:\ProgramData\Geocortex\Workflow\my-script.txt The specified file did not have a ".py" extension.
-2 Error executing script: No module named 'my-script' The specified file did not exist, or the Application Pool Identity did not have the necessary permission to read it.
-2 Error executing script: module 'my-script' has no attribute 'main' The specified file did not contain a function called main.
-2 Error executing script: unterminated string literal (detected at line 2) (my-script.py, line 2) There was a syntax error in the script.
-3 Error writing to output file: write() argument must be str, not int The main function in the specified file returned a value that was not a string.
109 (No error message) This is due to a known bug in Python 3.11 and later, relating to running py.exe using the Application Pool identity. Use the python configuration to specify the path to python.exe.