Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in RPA by (5.3k points)

after quite long inspection of this issue, I'm a little bit lost and I need community help. Let me explain the issue firstly:

I'm using the software (32-bit, let's call it UiPath) in Windows 7 (x64). From UiPath, I want to run python method by using Python 3.6 (64-bit) Interpreter. In UiPath, you can write your own activity programmed in C#. My goal is to write the activity (C# class), which will be able to run some python function with parameters and return me output (return) of this function.

I think, that cmd is not the good approach, maybe IronPython would help me, but can you give me any advice, please?

Thank you so much.

1 Answer

0 votes
by (9.5k points)

this is a project which is similar to your question 

here:https://github.com/Moobylicious/RunExternalProcessExample/archive/master.zip

This contains two console projects - one is 64 bit and one 32 bit. the 32-bit one calls the 64 bit by creating a process (command line in effect, but hidden). it then displays the output of this.

If you just run the solution as-is it should run the 32-bit console app, which will call the 64-bit one.

The main code to run an external process like this is below. The version in the sample is the Main() method of a console app, but you'd probably have to put this into a class library or whatever is required for a plugin/activity/thingy for your specific need.

public int RunProgram()

{

    //create  process to run an external program...

    System.Diagnostics.Process process = new System.Diagnostics.Process();

    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

    //hide it - don't spawn a CMD window

    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

    startInfo.FileName = "cmd.exe";

 

    //add command line params - guess these would be passed in somehow?

    var arg1 = "function_name";

    var arg2 = "input1";

    var arg3 = "input2";

 

    //Build command line.

    startInfo.Arguments = $@"/C c:\path\to\python\Some64BitProcess.exe {arg1} {arg2} {arg3}";

 

    //I don't know what the output of this will be.  whether it creates a file or

    //just prints stuff out on the command line (STDOUT). 

    //it is possible to capture STDOUT into a stream, but for simplicity I'll simply re-direct it into

    //an output file via the command line '>' which will overwrite any file already there.

    startInfo.Arguments += " > output.txt";

    process.StartInfo = startInfo;

 

    //Run process

    process.Start();

 

    //wait for it to complete - note, if this process pauses for user input at

    //any point this will basically just hang indefinitely.

    //you CAN pipe a 'Y' into it or something to avoid this(simulate a keypress),

    //but would be better to work out command line

    //switches that supress any waiting for user input.

    process.WaitForExit();

 

    //now, we can get results.

 

    var output = File.ReadAllText("output.txt");

 

    //Do things with output?

    Console.WriteLine(output);

    Console.ReadLine();

 

    //process.ExitCode is the Exit Code of the command.  This might be useful as it might

    //indicate specific errors encountered

    //by Python.

    return process.ExitCode;

}

 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 22, 2021 in Linux by sheela_singh (9.5k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...