-1

I have a simple script which is having the structure of:

if __name__ =='main':
    #do some logic
    #print to console the result

Now, I ran the script via the code using subprocess.Popen() method like this:

p = subprocess.Popen(
  ["python", path_of_script_to_run] + arguments_list, 
  stdout=subprocess.PIPE,
  stderr=subprocess.PIPE)

My question is how can I return a value from the running script to the calling script using return value, or other method.

I cannot use return under the context of if __name__ =='main': So does it mean I cannot pass a return value to be assigned in the calling script to the p variable?
I'm also want to try avoiding parsing the prints of the script run to the console..

JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • list + list in python is a list concatenated – JavaSa Jun 22 '16 at 13:37
  • You can use `exit` to set the return code. Suppose for instance that you want to return 2, just do `exit(2)`. – tdelaney Jun 22 '16 at 13:46
  • I want to pass something like a tuple of integers for example (1,5) – JavaSa Jun 22 '16 at 13:51
  • The best way to do this is to import the script to be run and call a method that returns the required data. Please see the answer by JF Sebastian http://stackoverflow.com/questions/30664263/return-value-from-one-python-script-to-another – gaganso Jun 22 '16 at 13:55
  • Its common to write the data to `stdout` and have the parent script parse it. But if the script is also pumping out other stuff that makes it hard to parse, you've got a problem. Perhaps that child script should have a "terse mode" flag that tells it to stop being chatty and just return an easily parsed result. – tdelaney Jun 22 '16 at 14:03

2 Answers2

1

Use communicate() to retrieve output of your script. And returncode to get the exit code.

p = subprocess.Popen(
  ["python", path_of_script_to_run] + arguments_list, 
  stdout=subprocess.PIPE,
  stderr=subprocess.PIPE)

(stdoutdata, stderrdata) = p.communicate()
exit_code = p.returncode

For your other script to return an exit code, you should use sys.exit(code).

import sys
if __name__ =='main':
    try:
        # do stuff
        sys.exit(0)
    except Exception as err:
        print(err)
        sys.exit(1)
Cyrbil
  • 6,341
  • 1
  • 24
  • 40
  • but how do I pass the return code from the context of the running script, trying to write return [something], cannot work as you get syntax error- 'return' out side of function – JavaSa Jun 22 '16 at 13:50
  • 1
    Use [`sys.exit(code)`](https://docs.python.org/3/library/sys.html#sys.exit) in your script – Cyrbil Jun 22 '16 at 13:52
0

You can capture the output in the calling script using subprocess.check_output():

from subprocess import check_output
result = check_output(args).decode()

To also capture standard error in the result, use stderr=subprocess.STDOUT.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • but how do I pass the return code from the context of the running script, trying to write return [something], cannot work as you get syntax error- 'return' out side of function – JavaSa Jun 22 '16 at 13:47
  • @JavaSa just `print` it? – Eugene Yarmash Jun 22 '16 at 13:50
  • thats exactly what I wanted to avoid, prints and parsing the prints are more weak that returning a value – JavaSa Jun 22 '16 at 13:56
  • But I guess I can't – JavaSa Jun 22 '16 at 13:56
  • 1
    @JavaSa, it's more that you aren't explaining what you mean by "returning" very clearly. If you want the Python interpreter to exit with that status that's easy, but it's not obvious that that's what you're asking for. – Charles Duffy Jun 22 '16 at 14:01
  • I want to be able to pass return value like you have a simple function `def foo(): return (1,4)` for example, and in the caller side i get the values like: `x,y = subprocess(.....)` Where x =1 and y =4 – JavaSa Jun 22 '16 at 14:09