- Overview
- Cryptography
- Database
- Java
- Python
- WebAPI
Manage Python Scripts
The following example explains how to automatically run a Python code created at runtime and write a file log. It presents activities such as Python Scope and Run Python Script. You can find these activities in the UiPath.Python.Activities package.
This is how the automation process can be built:
- Open Studio and create a new Process.
- Drag a Sequence container in the Workflow Designer.
-
Create a new Argument:
Argument Name
Direction
Argument Type
Default Value
in_PythonPath
In
String
Add the path of the folder where Python has been installed
-
Drag an If activity inside the Sequence container.
- Add the expression
String.IsNullOrEmpty(in_PythonPath) orelse not Directory.Exists(in_PythonPath)
in the Condition field.
- Add the expression
- Drag a Sequence container in the Then field of the If activity.
-
Drag a Message Box activity inside the Sequence container.
- In the Properties panel, add the expression
"Error"
in the Caption field. - Add the expression
"Pyhon Path not configured. Please configure path by setting the argument 'in_PythonPath'."
in the Text field.
- In the Properties panel, add the expression
-
Drag a Terminate Workflow activity after the Message Box activity.
- In the Properties panel, add the expression
"Input Arguments not correct"
in the Reason field.
- In the Properties panel, add the expression
-
Drag a Python Scope activity after the If activity.
- In the Properties panel, add the argument
in_PythonPath
in the Path field. - Select your Python version from the Version drop-down list (Python_36 in this example).
- In the Properties panel, add the argument
-
Drag a Run Python Script activity inside the Python Scope container.
-
In the Properties panel, add the following code snippet in the Code field:
String.Format( "import sys" + Environment.NewLine _ + "import os " + Environment.NewLine _ + "with open('{0}\logs.txt', 'w') as f: " + Environment.NewLine _ + " f.write('Starting script! \n')" + Environment.NewLine _ + " f.write('Computing!\n')" + Environment.NewLine _ + " f.write('Finishing script! \n')" + Environment.NewLine, _ Directory.GetCurrentDirectory.Replace("\", "\\"))
String.Format( "import sys" + Environment.NewLine _ + "import os " + Environment.NewLine _ + "with open('{0}\logs.txt', 'w') as f: " + Environment.NewLine _ + " f.write('Starting script! \n')" + Environment.NewLine _ + " f.write('Computing!\n')" + Environment.NewLine _ + " f.write('Finishing script! \n')" + Environment.NewLine, _ Directory.GetCurrentDirectory.Replace("\", "\\"))
-
-
Run the process. The automation runs the Python code and writes a log in a file named
logs.txt
.