Use the “Invoke PowerShell” Activity:
Property value below:
CommandText “python
‘C:\Path\To\YourScript.py’”
IsScript False (Because this is a single command)
Output Optional (if you want to capture Python’s output)
3. Example:
// Path to the Python executable
//string pythonPath = @"C:\Program Files\Python312\python.exe";
// Path to the Python script
//string scriptPath = @"C:\Users\admin_1\Documents\PythonScriptToExecute.py";
// Create a new process to run the Python script
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = pythonPath;
start.Arguments = scriptPath;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
// Start the process
using (Process process = Process.Start(start))
{
// Read the output from the Python script
using (System.IO.StreamReader reader = process.StandardOutput)
{
result = reader.ReadToEnd();
//Console.WriteLine("Output: " + result); // Print the result to verify
}
// Read any error messages from the Python script
using (System.IO.StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error: " + error);
}
}
}
Invoked code arguments
result → out → System.String → out_PythonScript
pythonPath → in → System.String → "C:\Program Files\Python312\python.exe"
scriptPath → in → System.String → "C:\Users\your_username\Documents\PythonScriptToExecute.py"
import sys
# Check if an argument was provided
if len(sys.argv) > 1:
param1 = sys.argv[1] # Get the first command-line argument
else:
param1 = input("Enter Name: ") # Fallback to user input if no argument is provided
print("Hello " + param1)
UiPath.Studio
UiPath.System.Activities.24.10.7
Add an Input Dialog activity with output System.String out_Name
// Path to the Python executable
//string pythonPath = @"C:\Program Files\Python312\python.exe";
// Path to the Python script
//string scriptPath = @"C:\Users\admin_1\Documents\PythonScriptToExecute.py";
// Create a new process to run the Python script
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = pythonPath;
start.Arguments = scriptPath;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
// Start the process
using (Process process = Process.Start(start))
{
// Read the output from the Python script
using (System.IO.StreamReader reader = process.StandardOutput)
{
result = reader.ReadToEnd();
//Console.WriteLine("Output: " + result); // Print the result to verify
}
// Read any error messages from the Python script
using (System.IO.StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error: " + error);
}
}
}
Invoked code arguments:
result → out → System.String → out_PythonScript
pythonPath → in → System.String → “C:\Program Files\Python313\python.exe”
scriptPath → in → System.String → string.Format("""C:\Marian\Python Projects\test\sample_script_with_input.py"" {0}", out_Name)