Python

This article provides information on the following topics:

Note: The Actions Quick Reference provides a complete list of actions that you can print for easy reference.

What is the Python Action?

The Python Action executes Python code authored by the user.

Python must be installed before a Python action can be executed. See Installing Python for more information.

Creating a Python Action

To create a new Python action, go to the Advanced section in the Action List and select the action called "Python". This is how it looks by default.

 The method

You can choose whether you would like to run "Python Code" or a "Python File":

  • Python Code = Will generate a .py file based on the content of the action and run it.

  • Python File = Will simply run the .py file you specify.

There are pros and cons to both approaches. With the "Python Code" approach, you can make use of Foxtrot variables, tokens, list lookups, etc., which is not possible with the "Python File". On the other hand, working with a big chunk of code in the Foxtrot window is not that easy, so for bigger pieces of Python code, it might be easier to have in separate .py files. We will go through both of the methods but mainly focus on the "Python Code" method.

Timeout

Foxtrot will at any time wait for the code to complete before proceeding with the Foxtrot project. This is very handy as the execution of the code might vary. But, what if your code will never end due a mistake in the code causing, for example, an infinite loop? The timeout is a feature added to make sure you do not risk running some infinite code that never ends. You can set the timeout to anything you would like, the default value is 30 seconds. Keep in mind that your code will be stopped by force if the timeout is reached.

Interpreter

The interpreter is the field where you can specify the Python interpreter you wish to use to execute your Python code. By default, it will simply say "python.exe". This is something you can use if you only have one version of Python installed. But, if you have multiple versions of Python installed, for example, Python 2 and 3, you will have to specify the full path to the Python .exe you would like to use. This is also relevant if you are using virtual environments in your Python setup. If you are a completely new user and just installed Python, you can just leave it with "python.exe".

Code

If you select the "Python Code" method, here is where you will write your Python to execute. With the "Python Code", Foxtrot will basically take the code you write and generate a temporary .py file, execute the file using the specified interpreter, and delete it again. The great thing about this approach is that it supports using Foxtrot variables, tokens, list lookups, etc.

File

If you select the "Python File" method instead, you will have to simply specify the path to the .py file you wish to execute. Foxtrot will execute the file "as-is", therefore, this approach will NOT support the use of any Foxtrot variables, tokens, list lookups, etc. in the code itself.

Save to

Lastly, you can decide whether you wish to save the result (output) of the code to a Foxtrot variable. We definitely recommend that you always do this, however, it is not mandatory. The output will be:

  • The output of any "print" statements

  • The output of any error messages

Python Code Example

Here is an example of a simple Python script that prints a message. In this example we will save this output into a variable. If you do not have any "print" statements in your code and there are no errors, the output will be empty.

First create a Foxtrot variable called "Output" and write one line of code like this:

print('Hello Foxtrot World')

If everything is setup correctly, you should get an output in your "Output" variable with the value from the "print" call.

Now, let us try to force an error! Simply "forget" to surround the value of the print statement with '' like this:

print(Hello Foxtrot World)

This should not work - but be aware, you will not get a traditional Foxtrot error, any error message will be described in the selected output variable. In this case, the output will be:

That is the very basics of the Python action in Foxtrot.

Top of Page

Python File Example

Using a Python file works exactly the same if using the "Python Code" method. For example, let us open Notepad and create a new file containing the same line of code. You can use any editor you like to create this file such as Notepad++ or PyCharm, but we use Notepad here just for the sake of simplicity.

Make sure to save it as a ".py" file.  

Now, close the Notepad application and let us try to run this file from Foxtrot:

You should get exactly the same result as before in your "Output" variable:

Setting Foxtrot variables from Python

The Python action comes with a very nice feature if you need to set multiple variables in Foxtrot with one Python action. As we have explored so far, the information saved to the output variable is all the "print" statements or an error message. But, in many cases you actually need to set multiple different Foxtrot variables from one Python action. You can do this using the special Foxtrot function called "RPAEngine.SetVar". The syntax is:

RPAEngine.SetVar("Foxtrot_Variable_Name", Python_Value)

Here is an example of how this works:

So, as you can see, we can set multiple set the value of multiple variables in Foxtrot using one Python action, which is very useful in many cases. Typically, you will use the "print" statements to log certain events throughout your code so that you can evaluate whether your code was successful after execution, while you use the "RPAEngine.SetVar" feature to set the value of specific variables.

Coding Examples

Below you may find some screenshots of simple examples of other use-cases with the Python action in Foxtrot.

Parsing JSON

Writing Text Files

Calling Functions