Execute Python Script

Use the Execute Python Script activity to run a Python script within an Automation workflow.

Modules

The Execute Python Script activity has a pre-defined set of modules available to use. You cannot add your own modules using pip or other package managers. To see which modules are available, you can run a workflow with the Execute Python Script activity and the following script:

# Output a list of available modules
 
help("modules")

The activity output will contain a list of available modules.

Script Output

A Python script can provide its output in two ways: printed to the Response Body of the activity or through script queries.

Response Body

If you use functions such as print() in your script, all of the printed output will be put in the Response Body variable. This can be useful for printing a variety of output throughout your script, but keep in mind that all of the output will be treated as a single string.

Script Queries

If you want to extract the value of one or more of the script variables, you can do that using script queries. These queries allow you to extract the value of a Python variable and make it available in the activity’s output. The benefit of using this approach is that you can extract multiple values and define the data type of each value.

Usage

Complete the following properties to use this activity:

  • Python Query - Specify the following information or click the Variable Reference icon to choose a variable:
    • Script Arguments - Click Add to enter any arguments you want included with your script.
    • Script to Execute on Target - Enter the script you want to run on the target.
    • Script Output Variables - Click Add to specify the following information or click the Variable Reference icon to choose a variable:
      • Script Variable - Enter the name of the variable.
      • Property Name - Enter the name of the property.
      • Property Type - Click the drop-down list and choose the property type.

Best Practices

The general idea to keep in mind when you write python scripts is to keep the code simple, straightforward and more readable. The other person reading your code should be able to tell right away what is going on and where the flow is going.

Use 2-space indentation in favor of the more common 4-space, since it's the default format the Python activity operates in, thus make it more usable for further modifications by different users.

Use camelCase for names in python activities.

For more than one input argument, use multiline statements to deconstruct and limit the slice explicitly from both start and end.

We don't use trailing commas for multiline statements such as:

  • lists

  • tuples

  • function parameters

Separate each import statement with a new line.

Caution, all internal XDR variable types are automatically cast into string prior to be passed to the python code!

Input validation, remember that all the input variables of Execute Python Script activities are of type string. Validate your inputs accordingly.

Script output variables: Use camelCase for Property Names, the same as names in the Python activity. Avoid duplicates with input, local, or output variables.

If your script is preparing a payload for a Web Service HTTP Request activity, then the output payload variable should be named payload.