activities
latest
false
- Overview
- Cryptography
- Database
- Java
- Python
- WebAPI
Invoking a Python Script
Developer Activities
Last updated Oct 22, 2024
Invoking a Python Script
To exemplify how to use this activity package, we have built an automation process to
invoke a Python script which defines two operations, Add and Subtract. One
of the methods, Add, is then called from the script and given a list of integer
variables as input parameters. The result, stored in a proprietary
PythonObject
variable, is then converted into a .NET data type to
be further used in Studio.
Note: The Python version
you want to use has to be installed on the machine you want to run the process on. This
step-by-step guide is based on a default installation path of Python 3.12.
This is how the demo can be built:
- Open Studio and start a new Process.
- Drag a Multiple Assign
activity to the designer panel and add the following variables:
- Add the
localAppDataPath
variable with the value to saveEnvironment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
. - Add the
pythonPath
variable with the value to savelocalAppDataPath+"\Programs\Python\Python312\"
. - Add the
pythonLibraryPath
variable with the value to savelocalAppDataPath+"\Programs\Python\Python312\python312.dll"
.
- Add the
- Add a Log Message activity to the workflow.
- Add the
"The installation path of Python 3.12 was set as: " + pythonPath
expression in the Message field.
- Add the
- Add a Python Scope
activity.
To properly set up the Python Scope activity for Python 3.12, it is required to configure both the Library path and the Path properties.
- Under the Library path
property, enter the
pythonLibraryPath
variable created in the Multiple Assign activity. - Under the Path
property, enter the
pythonPath
variable created in the Multiple Assign activity.
- Under the Library path
property, enter the
- Drag a Load Python Script
activity in the Do section of the Python Scope.
- Add the file path to the
MathOperations.py
Python script in the File property. - Create a new
PythonObject
variable, calledmathOperations
, and add it into the Result property field.
- Add the file path to the
-
Add an Invoke Python Method activity and configure its properties as follows:
- Input Parameters - Add
the
new Object() {2,3}
expression to this property field. - Instance - Add the
mathOperations
variable to this property field. - Name - Add the name of
the method inside the script you want to use, in this case
add
. - Result - Create a new
PythonObject
variable, calledsumResultPython
, and add it to this property field.
- Input Parameters - Add
the
-
Add a Get Python Object activity and configure its properties as follows:
- PythonObject - Add the
SumResultPython
variable to this property field. - TypeArgument - Select
Int32
from the drop-down list. - Result - Create a new
Int32
variable, calledsumResult
, and add it to this property field.
- PythonObject - Add the
-
Add a Message Box activity at the end of the project and configure it to display the
sumResult
variable.Your project should look like this:
-
Run the process. The Message Box displays the result of the Add method.