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:
- General
- Display Name - This name is what's displayed for the activity in the Workflow Editor.
- Description - A meaningful description may be helpful.
- Activity timeout (seconds) - Enter the amount of time in seconds that Automation will attempt to complete this activity before it times out.
- Continue Workflow Execution On Failure - Check this check box to continue the workflow if this activity fails (optional).
- Skip activity execution - Check this check box to bypass running the activity during the workflow execution (optional).
- Python Query
- Script arguments - Script arguments in Python are values passed to the script when it is executed. They enable you to provide input to the script, control its behavior, or specify parameters without modifying the code directly. These arguments are accessed within the script using the sys.argv list. Click Add to enter any arguments you want included with your script. You can click the
(Variable Reference) icon and choose the variables to enter, including those from other activities to be referenced in the script.
- Script to execute on target - Enter the script you want to run on the target. To reference variables directly in the script, navigate within the script and insert variables where desired by clicking the
(Variable Reference) icon and choosing a property from an activity, for example.
- Script Output Variables - Click Add to specify the following information:
- Script Variable - Enter the name of the variable, or click the
(Variable Reference) icon and choose the variable.
- Property Name - Enter a name for the property.
- Property Type - Click the drop-down menu and choose the data type of the value.
- Script Variable - Enter the name of the variable, or click the
- Script arguments - Script arguments in Python are values passed to the script when it is executed. They enable you to provide input to the script, control its behavior, or specify parameters without modifying the code directly. These arguments are accessed within the script using the sys.argv list. Click Add to enter any arguments you want included with your script. You can click the
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 multi-line statements such as:
-
lists
-
tuples
-
function parameters
Separate each import statement with a new line.
All internal XDR variable types are automatically cast into string prior to being passed to Python code.
Input validation: 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.