activities
latest
false
UiPath logo, featuring letters U and I in white

Developer Activities

Last updated Dec 11, 2024

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.

Note: Before running any Python workflow, make sure that you have Python3 installed.

This is how the automation process can be built:

  1. Open Studio and create a new Process.
  2. Drag a Sequence container in the Workflow Designer.
  3. 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

  4. 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.
  5. Drag a Sequence container in the Then field of the If activity.
  6. 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.
  7. 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.
  8. 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).
  9. 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("\", "\\"))
  10. Run the process. The automation runs the Python code and writes a log in a file named logs.txt.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.