Description
This activity lets you pass data using Studio Pro to execute Python scripts. You can learn more about how to run scripts written in Python in this article.
How to use it?
There are two ways to perform this operation, depending on the amount of data.
First Method: String
You can pass a string to Python as a parameter. To demonstrate this, let us look at the following workflow.
It is important to note that your string must be wrapped in double quotes, otherwise Python will process your string incorrectly. Using the given string, you can create a variable in Studio Pro as shown below.
We also need to add a Python script as a test case
import sys
print(sys.argv[1])
It should be noted that
-
sys
library should be imported in your Python script:import sys
. -
Once it has been imported, any argument can be called:
sys.argv[{{argument number}}]
.
The result of the workflow is
However, you can get the same result if you add double quotes to a variable in Parameters as follows '"' + {{variable name}} + '"'
. For this reason, we no longer need to manually add double quotes to a string. This operation should look like below.
Multiple values can be sent as needed. Make sure to wrap them correctly and use proper indices. As an example, let us consider the expression: '"' + test + '" "test_value"'
. In this case, the value of test
is the argv[1]
, and for the test_value
is argv[2]
.
You can also pass two variables as arguments: '"'+test1+'" "'+test2+'"'
.
Second Method: A large amount of data
At the moment, there is only one possible way to pass a large amount of data, for example a JSON data file. In this case, you need to extract data using Studio Pro and then use it in Python. Let us look at the following example to understand how to deal with such a problem.
-
Read emails and save the value to a variable (in the JSON format).
-
Create a file in the JSON format using the result of the previous step.
-
Prepare a Python script and use it with Execute Python activity as follows.
-
Use Assign value to variable activity to save data returned from Python and use it further.
In the given case, we have created the following workflow.
We also need the Python script to import JSON data.
#Import json library
import json
# Import file and encode/decode JSON
with open('C:\\Users\\ElectroNeek\\Desktop\\test_case\\test.json') as file:
data = json.load(file)
#returns data back to Studio Pro for further usage
print(data[0]['from'][0]['address'])
The result of this test workflow is simply an email address.
Troubleshooting
There are two main errors that may occur while a bot is running.
1. The first error message is thrown if a wrong path was used for a Python script.
In order to fix this problem, you need to check that an absolute path is being used, for example
2. The second error message may appear if the file name or one of the folders in the path contains a space in the name.
In order to solve this problem, you need to wrap the whole path in double quotes, for example:
3. Тhe third possible error can be displayed as follows:
In this case you should check the "Path to venv folder" parameter, because the error means that the path to the venv folder is not specified correctly: the folder does not exist or it does not contain Python libraries. Note an example of the contents of the venv folder: