How to invoke a Python script in UiPath using Invoke Code activity?

Hello,

I want to invoke a Python script with input parameters (string, int etc).

How do I invoke using Invoke Powershell activity in UiPath?

I’m using the latest version of Python and don’t want to use Invoke Python scope.

Thanks in advance for your help!

Hi @voranir

Is there a specific reason why you don’t want to use direct python activities for this purpose?

If you only want to do it via powershell, please try code like below in command text of invoke powershell activity and see if this works

python “path\to\your\script.py” arg1 arg2

Regards
Sonali

Hello,
you can use directly an Invoke power shell activity and type “python ./pythonscript.py” input1 input2

Or
you can use an use application activity as shown in the screenshot, and pass the command in a type into activity

  1. 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:

CommandText = “python ‘C:\Users\YourName\Documents\my_script.py’”
IsScript = False

Hope this help

Hi @sonaliaggarwal47

Thank you for the reply.

The reason we are not using python activities is because the latest Python version is not compatible intgrating with UiPath.

Try this approach with Invoke Code for C#:

UiPath.Studio.24.10.14
UiPath.System.Activities.24.10.7

Install

Import these namespaces:

System
System.Diagnostics

Invoke Code activity for Language C#:

        // 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"

PythonScriptToExecute.py content

def add_numbers(a, b):
    return a + b

# Example usage
out_results = add_numbers(5, 3)
print(out_results)

Results:

Hi @marian.platonov,

Thank you for sharing this.

I have a script which needs input parameters.

param1 = input (“Enter Name”)
print (“Hello” + param1)

How do I pass the argument in the code?

Thanks

Python code:

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

Invoke Code activity for C#

    // 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)

Results of execution:


Hi @marian.platonov,

Thank you for sharing the details.

I’m getting the below error

I’m using UiPath.System.Activities - 24.10.6

Share the project and the python code.
Did you follow the same steps as above, or do you skipped something?

Hi @marian.platonov,

It’s working now.

I was not passing the command line arguments in the python script.

Thank you for the solution.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.