UiPath Documentation
activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

开发者活动

上次更新日期 2026年5月21日

管理 Python 脚本

以下示例说明如何自动运行在运行时创建的 Python 代码并写入文件日志。它显示了诸如“Python 作用域”“运行 Python 脚本”之类的活动。您可以在“UiPath.Python.Activities ”包中找到这些活动。

备注:

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

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”

  2. 将“序列”容器拖动至“工作流设计器”

  3. 创建新参数:

    参数名称方向参数类型默认值
    in_PythonPath输入字符串添加已安装 Python 的文件夹的路径
  4. Drag an If activity inside the Sequence container.

    • 在“条件”字段中添加表达式“String.IsNullOrEmpty(in_PythonPath) orelse not Directory.Exists(in_PythonPath)”。
  5. 将“序列”容器拖动到“If 条件”活动的“Then”字段中。

  6. Drag a Message Box activity inside the Sequence container.

    • In the Properties panel, add the expression "Error" in the Caption field.
    • 在“文本”字段中添加表达式 "Pyhon Path not configured. Please configure path by setting the argument 'in_PythonPath'."
  7. “终止工作流”活动拖动至“消息框”活动之后。

    • In the Properties panel, add the expression "Input Arguments not correct" in the Reason field.
  8. “Python 作用域”活动拖动至“If条件”活动后。

    • 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. 运行流程。 自动化运行 Python 代码,并在名为 logs.txt的文件中写入日志。

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新