The Python action is currently only available in beta. Python is an open-source interpreted, high-level, general-purpose programming language that can be very powerful for automation. In Foxtrot, you can make a Python action that either executes the code that you have in a .py file or the code that you write directly in the action form in Foxtrot. When you run Python actions in Foxtrot, whether it is the file or code method, Foxtrot will utilize the Python interpreter you have installed on your machine. Therefore, it is a prerequisite that you install Python. If you have not installed Python already, you recommend that you read our guide on how to do so:
After installing Python, as also mentioned in the article, we recommend that you also set up an application like PyCharm that you use to actually write the code you wish to execute in Foxtrot. Foxtrot is not designed to act as a text editor, therefore, you might find it easier to write your actual code in an application like PyCharm or similar and then copy-paste the code to your Foxtrot actions.
In this article, we will take a look at how you use the Python action in Foxtrot. We will not in details look at how you write Python code. If you have zero experience with Python, we recommend that you study some basics before using it in Foxtrot. Python is one of the most used programming languages in the world, therefore, you can easily find a lot of great resources online. Here are some that we find great:
- https://www.youtube.com/watch?v=rfscVS0vtbw
- https://www.youtube.com/watch?v=_uQrJ0TkZlc
- https://www.youtube.com/playlist?list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7
Either before or after completing this guide, you may also read our other Python guides:
Getting started
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
As already mentioned, 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
So, if you do not have any "print" statements in your code and there are no errors, the output will be empty. Here is an example. Make sure to 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. It works exactly the same if using the "Python File" method. Just as a quick example, let us open Notepad and create a new file containing the same line of code. Normally, you would use PyCharm or a similar application, 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:
How to set Foxtrot variables
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.
Examples
As mentioned in the beginning of the article, you may find other relevant guides on our website on specific use-cases for the Python action. 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
Comments
0 comments
Please sign in to leave a comment.