Python connector methods

  • Release version: Zurich
  • Updated August 14, 2025
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Python connector methods

    The Python connector methods in RPA Desktop Design Studio allow ServiceNow customers to execute custom Python scripts or files within automation workflows. These methods facilitate integrating Python logic directly into your automation processes, enhancing flexibility and capability.

    Show full answer Show less

    Prerequisites

    • Configure the Python connector properly before use.
    • Understand the limitations of Python scripts within the connector.
    • Ensure that Python is installed locally when using certain methods.

    Execute Method

    This method executes Python scripts that are pre-configured in the connector settings. To use Execute:

    • Configure the connector and define classes and methods in the configuration window.
    • Select the desired class and method from the respective lists in the component settings.
    • Optionally, select standalone functions (not within classes) by enabling the Show Functions option.
    • Validate and apply the configuration to create a Data Out port named Result, which returns the script output.
    • Test execution by running the method directly from the design environment.

    This method returns the output as a string through the Result data port.

    InvokeScript Method

    The InvokeScript method runs Python scripts located on the local machine using the command line. Key points include:

    • Specify the full command line to execute the Python script, including the Python interpreter path and script file path, with any arguments separated by spaces.
    • Ensure the Python script runs error-free independently before invoking it.
    • Returns the script’s output as a string through the Result data port.

    Practical Use and Benefits

    • Enable integration of complex Python logic, calculations, or utilities into ServiceNow automation workflows.
    • Use pre-written Python classes, methods, or standalone functions seamlessly within RPA processes.
    • Execute scripts locally with full control over the Python environment and script arguments.
    • Receive script outputs directly in the workflow for further processing or decision-making.

    Example

    A sample Python script demonstrates greeting the user, returning the current time, and performing arithmetic operations via a Calculator class. This illustrates how you can organize Python code for use with these connector methods:

    import datetimedef greetuser(name): print(f"Hello, {name}!")def getcurrenttime(): print(f"Current time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")class Calculator: def init(self, a, b): self.a = a self.b = b def add(self): print(f"Addition: {self.a + self.b}") def multiply(self): print(f"Multiplication: {self.a self.b}")def runall(): greetuser("Abel.Tutor") getcurrenttime() calc = Calculator(5, 3) calc.add() calc.multiply()if name == "main": runall()

    This example can be executed through either method according to your integration needs.

    Execute custom Python scripts or files as part of an automation workflow by using the methods of Python connector in RPA Desktop Design Studio.

    Prerequisites for using the Python connector methods

    Configure the Python connector. For more information, see Configure the Python connector.

    Familiarize yourself with the limitations of Python script. For more information, see Limitations of Python script in Python connector.

    Execute

    Executes Python scripts set up in the configuration window. Before executing the method, you must configure the connector. See Configure the Python connector.

    To execute, do the following steps.
    1. Select the component and click the method settings icon (Method settings icon) or double-click the component head bar.
    2. From the Classes list, select the class defined during the configuration of the Python connector.

      For example, Math.

    3. From the Methods list, select the method defined during the configuration of the Python connector.

      For example, add.

    4. Select the Show Functions check box and select a function from the Functions list.

      Functions are those methods that are not written within a class. For example, banner.

      Figure 1. Show Functions check box
      Banner function is selected from the Functions list.
      Figure 2. Function in Execute Method
    5. Click Validate.
    6. Click OK.

      A Data Out port (Result) is created to return the output.

    7. To test the method, right-click the Execute method and select Run From Here.
    Table 1. Execute method parameters
    Parameter Description Data port type Data type
    Result Returns the output of the script.
    Figure 3. Result of the Execute method
    "Welcome Abel Tutor" is displayed in the Data Out port of the Execute method.
    Data Out String

    InvokeScript

    Runs the Python script on local computers and gets the output. Ensure that the Python file is available in the file path provided.

    Ensure that a Python version is installed on your machine. You must ensure that the Python script is running without errors.

    Figure 4. InvokeScript method
    InvokeScript method.
    Table 2. InvokeScript method parameters
    Parameter Description Data port type Data type
    Script Returns the output of the script.

    The command specified in the Script parameter for execution via the InvokeScript component is same as the command line that is used to run in a command prompt.

    Provide a file path for the Python script. For example, python "C:\Users\abel.tutor\Downloads\utility_runner.py"

    For example, py -3 "C:\Python testing\project1\main.py" Abel it has <python with version> <python file path> <arguments> format. For adding multiple arguments, use space.

    Example of a Python script that greets the user, returns current time, and performs calculations of add and multiply.
    # utility_runner.py
    
    import datetime
    
    # Top-level function
    def greet_user(name):
        print(f"Hello, {name}!")
    
    # Another top-level function
    def get_current_time():
        print(f"Current time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    # Class with methods
    class Calculator:
        def __init__(self, a, b):
            self.a = a
            self.b = b
    
        def add(self):
            print(f"Addition: {self.a + self.b}")
    
        def multiply(self):
            print(f"Multiplication: {self.a * self.b}")
    
    # Run all functions and methods in sequence
    def run_all():
        greet_user("Abel.Tutor")
        get_current_time()
    
        calc = Calculator(5, 3)
        calc.add()
        calc.multiply()
    
    if __name__ == "__main__":
        run_all()
    
    Data In String
    Return Output of the script that is returned.
    Figure 5. Result of the InvokeScript method
    Result is displayed in the Data Out port of the InvokeScript method.
    Data Out