Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear ALL:

I am using .NET3.5 C# run a python script and try to get the return value from python

I put the python script under disk C://.

Then in C# run the python script with:

ProcessStartInfo Info = new ProcessStartInfo();

                        Info.FileName = "getvalue.py";

                        Info.WorkingDirectory = @"c:";

                        Process Star = Process.Start(Info);


Then how could I get the value from python script.

For example python script return a value "3".

Any tips welcomed and appreciated.


Regards

Kay
Posted

1 solution

There are two ways of doing it.

First one is very limited and based on exit code. On Windows and Unix-like systems, all applications return integer code which defaults to 0. Traditionally, 0 means "everything is OK", but it could be anything. With Python, exit code is returned by assigning an integer value to the special variable $?. Please see:
http://stackoverflow.com/questions/285289/exit-codes-in-python[^].

On the side of your .NET parent process, when the child process is terminated, you can pick up the exit code using the property System.Diagnostics.Process.ExitCode:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx[^].

You can synchronize a thread of your parent process with the termination of your child process using the methods System.Diagnostics.Process.WaitForExit:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

Please note that these methods are blocking, so they are not suitable for calling, say, from a UI thread — you might need to execute all your code using System.Diagnostics.Process from a separate thread, at least in the case if your Python script takes considerable time to complete.

The second approach is more universal but a bit more awkward and is based on console output. You can echo some output text to the console in your Python script. Your parent process needs to pick up this text, parse and process it the way you need. To do that, you need to redirect output from the child process (Python script, in this case) to a stream and read the content of this stream. This is done by redirection of StandardOutput, and, just in case, StandardError streams. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx[^].

Both MSDN help pages referenced above show how to do the redirection on the code samples. Please see — they are descriptive enough.

—SA
 
Share this answer
 
v5
Comments
fcapba 2-May-12 2:21am    
Thanks a lot SAKryuKov!! Very detailed explanations, appreciated.
Sergey Alexandrovich Kryukov 2-May-12 9:51am    
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900