One Way how to use Python seamlessly with UiPath

Python is a very common and consolidated programming language. There are a lot of solutions in Python and often they can be reused in the context of UiPath. But the call of the interpreter and the passing of arguments is often difficult. Here a solution which bases on my PowerShell approach with UiPath. It uses exactly the same method. But in this case I start a new process with the Python interpreter. As arguments I transfer the Python script file name and a message text to the Python script. The text and error are redirected to variables. On this way we can call the Python interpreter without a visible window but get all information from the interpreter.

#-Begin-----------------------------------------------------------------

Param(
  [String]$Message
)

#-Function CallPython---------------------------------------------------
Function CallPython {

  Param(
    [String]$PythonFile
  )

  $PythonCmd = "c:\Program Files\Python37\python.exe"
  [System.Diagnostics.ProcessStartInfo]$startInfo = `
    [System.Diagnostics.ProcessStartInfo]::new();
  $startInfo.FileName = $PythonCmd;
  $startInfo.Arguments = "$($PythonFile) $($Message)";
  $startInfo.UseShellExecute = $False;
  $startInfo.CreateNoWindow = $True;
  $startInfo.RedirectStandardOutput = $True;
  $startInfo.RedirectStandardError = $True;
  [System.Diagnostics.Process]$Proc = `
    [System.Diagnostics.Process]::new();
  $Proc.StartInfo = $startInfo;
  If($Proc.Start() -eq $False) {
    Return;
  }
  [System.IO.StreamReader]$stdOut = $Proc.StandardOutput;
  [System.IO.StreamReader]$stdErr = $Proc.StandardError;
  If(($stdOut -eq $null) -or ($stdErr -eq $null)) {
    Return;
  }
  $Result = $stdOut.ReadToEnd();
  If($Result.Length -eq 0) {
    Return $stdErr.ReadToEnd();
  } Else {
    Return $Result;
  }

}

#-Sub Main--------------------------------------------------------------
Function Main {
  $Result = CallPython -PythonFile "C:\Dummy\CallPythonFromPS.py";
  $Result;
}

#-Main------------------------------------------------------------------
Main;

#-End-------------------------------------------------------------------

Here now the Python script. It delivers only the message text and a view technical information about Python.

# -*- coding: iso-8859-15 -*-
#-Begin-----------------------------------------------------------------

#-Imports---------------------------------------------------------------
import platform, struct, sys

#-Sub Main--------------------------------------------------------------
def main():
  print(sys.argv[1], "\nPython", platform.python_version(), "on", 
    platform.system(), "(" + platform.architecture()[0] + ")")

#-Main------------------------------------------------------------------
main()

#-End-------------------------------------------------------------------

Here you see the result of my test. In the output window you see the return value from Python, which is on the one hand the Message variable from UiPath and on the other hand the information about the Python version, system and architecture.

I admit that it is a bit adventurous, but it works very well and reliably. It gives us the possibility to integrate Python seamlessly into UiPath. The passing of parameters of more complex data types would be even closer to investigate, but the basic approach does not change.

2 Likes