[RPA]: Using The Python Activity in UiPath

Level of Difficulty: Beginner.

This post will assist in using the Python Activity in UiPath which allows Python scripts to be invoked and executed from UiPath. Not only can you execute Python scripts, but you can also pass parameters through to the Python method and assign the result to a UiPath variable which can be used later in the RPA process.

Please Note: The Python Scope only supports Python versions 3.6 or earlier

What are the steps?

The steps that we will be following are:

  1. Create Python Script
  2. Install the UiPath.Python.Activities library
  3. Add a “Python Scope” Activity
    1. Point the Python Scope to the Python Interpreter through the path property
    2. Ensure that the version and target properties are correctly configured
  4. Add a “Load Python Script” Activity
    1. Add the file path to the Python script
    2. Create and assign the result variable to the result property of the activity
  5. Add an “Invoke Python Method” Activity
    1. Assign the input parameters
    2. Assign the instance name
    3. Assign the method name
    4. Create and assign the result variable to the result property of the activity
  6. Add a “Get Python Object” Activity
    1. Assign Python Object to the Invoke Python Method result variable
    2. Assign the TypeArgument to the Python method return type
    3. Create and assign the result variable to the result property of the activity
  7. Add a Message Box that displays the Python result

Deep Dive

Let’s dive deeper into the steps listed above.

Create Python Script

Before creating a new UiPath Process, a test Python Script (named BasicMath.py) should first be created (it is advised that the Python Script and the virtual environment exists in the same root folder as the UiPath Process):

# takes a list of numbers as a parameter
def addition(numbers):

    sum = 0

    for number in numbers:

        sum += number

    return sum


# takes a list of numbers and the total to be subtracted from as a parameter
def subtraction(numbers, total):

    for number in numbers:

        total -= number

    return total


# takes the state (add or subtract), list of numbers and the total to be subtracted from (if state supports it) as a parameter
def main(state, numbers, total):

    if state == "subtract":

        return subtraction(numbers, total)

    elif state == "add":

        return addition(numbers)

Install the UiPath.Python.Activities library

Create a new UiPath Process, add a sequence to the Main.xaml file and click “Manage Packages” as seen below:

Ensure that you are focused on “All Packages” when searching for UiPath.Python.Activities:

Select the UiPath.Python.Activities library and click on the Install button. After clicking the install button, click on Save and Accept when prompted:

Now that we have the library installed, we can wire our process up to Python.

Add a “Python Scope” Activity

Search for “python” in the Activities panel and drag the “Python Scope” over into the Sequence sitting in the Main.xaml file.

Now… To link the scope to the interpreter, you’ll need to provide the path to the interpreter in the “path” property of the Python Scope:

To find the path of your default Python interpreter, create a basic Python script and run the following commands:

import os
import sys
print(os.path.dirname(sys.executable))

This can also be done through the command prompt:

Ensure that your target and version are correctly configured in the properties of the Python Scope:

Add a “Load Python Script” Activity

Time to grab a “Load Python Script” activity from the activities pane and drag it into the Python Scope:

Add the path to the Python Script in the “File” property of the activity:

Click on the “Result” property and hit Ctrl+K to create a new variable (of PythonObject type) named basicMathScript

Drag the “Invoke Python Method” activity over into the Python Scope:

Create a variable named inputNumberList (of type Array of [T] where T is int32) and assign the default value to {1,2,3,4,5}:

Assign the following values to the corresponding “Invoke Python Method” property:
Input Parameters: {“add”, inputNumberList, 0} (state, numbers, total)
Instance: basicMathScript (which is the result variable of the Load Python Script Activity)
Name: “main” (which is the name of the method that should be executed)
Result: hit Ctrl + k to create a variable (of type PythonObject) named pythonInvokeResult

Add a “Get Python Object” Activity

Drag the “Get Python Object” activity over into the Python Scope:

Assign the following values to the corresponding “Get Python Object” property:
Python Object: pythonInvokeResult (which is the “Invoke Python Method result)
TypeArgument: Int32 (This refers to the result type returned by the Python Method)
Result: hit Ctrl + k to create a variable (of type Int32) named pythonMainResult

Add a Message Box that displays the Python result

Search for the Message Box activity and drag it over into the Python Scope:

Assign the Text property of the Message Box to pythonMainResult.ToString

Upon execution, you should see the following Message Box appear with the result:

To test the subtraction method, feel free to play around with the parameters that are sent into the “Invoke Python Method” activity:

The final process should look like this:

The above solution is available on Github

If you have any questions, issues or feedback, drop a comment below or reach out to jacqui.jm77@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: