Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the return value of a Python script

Tags:

python

c#

I have an external C# program which executes a Python script using the Process class.

My script returns a numerical code and I want to retrieve it from my C# program. Is this possible?

The problem is, I'm getting the return code of python.exe instead of the code returned from my script. (For example, 3.)


1 Answers

The interpreter does not return the value at the top of Python's stack, unless you do this:

if __name__ == "__main__":
    sys.exit(main())

or if you make a call to sys.exit elsewhere.

Here's a lot more documentation on this issue.

like image 180
Mark Rushakoff Avatar answered Feb 28 '26 19:02

Mark Rushakoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!