activities
latest
false
Developer Activities
Last updated Sep 12, 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:

  1. Open Studio and start a new Process.
  2. Drag a Multiple Assign activity to the designer panel and add the following variables:
    • Add the localAppDataPath variable with the value to save Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).
    • Add the pythonPath variable with the value to save localAppDataPath+"\Programs\Python\Python312\".
    • Add the pythonLibraryPath variable with the value to save localAppDataPath+"\Programs\Python\Python312\python312.dll".
  3. 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.
  4. 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.
  5. 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, called mathOperations, and add it into the Result property field.
  6. 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, called sumResultPython, and add it to this property field.
  7. 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, called sumResult, and add it to this property field.
  8. 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:



  9. Run the process. The Message Box displays the result of the Add method.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.