Please enable JavaScript to view this site.

Version:

Navigation: Server Workflows

Execute Python scripts

Prev Top Next More

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:

end with a ".py" file extension.

be a valid Windows file path for a file that exists.

be in a location that the Application Pool Identity has read access to.

The file contents must:

include a function called main, which must:

accept one parameter, which is an array of strings. The first member of the array is the file path of the script itself. All subsequent members are the values specified by the Arguments input.

return a string, or nothing at all.

Examples

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

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.

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.

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.

Anything you write to sys.stderr will be included in the error output.

The value you pass in to sys.exit() will be assigned to the exitCode output.

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.

© 2024 VertiGIS North America Ltd. All Rights Reserved. | Privacy Center | Imprint
Documentation Version 5.42 (220a4365)