-2

I'm trying to run my test_script.py in main_script.py with subprocess. test_script.py is a siple sum program, and main_script.py should call it with 2 arguments, and catch output. Here is the code:

test_script.py

a = int(input())
b = int(input())

print(a+b)

main_script.py

import subprocess
subprocess.check_output(['python', 'test_script.py', 2,3])

This is the error im getting:

Traceback (most recent call last):
  File "C:/Users/John/Desktop/main_script.py", line 2, in <module>
    subprocess.check_output(['python', 'test_script.py', 2,3])
  File "C:\Python34\lib\subprocess.py", line 607, in check_output
    with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
  File "C:\Python34\lib\subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1085, in _execute_child
    args = list2cmdline(args)
  File "C:\Python34\lib\subprocess.py", line 663, in list2cmdline
    needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
Nobbie
  • 13
  • 2
  • 3
  • http://stackoverflow.com/questions/14078117/how-do-you-use-subprocess-check-output-in-python – Saeid Nov 27 '15 at 14:35
  • IMHO, this it is poor practice to call one script from another. Better to simply `import test_script` from `main_script.py`. Or create create functions/classes in `test_script` and invoke them from `main_script.py` like `test_script.my_function(2,3)` – Alastair McCormack Nov 27 '15 at 14:38

3 Answers3

1

Actually the subprocess check output command only returns the value. So, you need a variable to store it in. So you can try this:

result = subprocess.check_output("python test_script.py", 
 stderr=subprocess.STDOUT, shell=True)
print(result)
0

All parts of argument must a string. Do the following instead:

subprocess.check_output(['python', 'test_script.py', "2", "3"])

If this command fails to run, you'll get an exception. To catch it and see the output:

try:
  subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
  print e.output

Your second script will fail because it's expecting input from the stdin, while your master script is sending the 2 and 3 as arguments. Investigate sys.argv

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • I'm getting this error `subprocess.CalledProcessError: Command '['python', 'test_script.py', '2', '3']' returned non-zero exit status 1` – Nobbie Nov 27 '15 at 14:46
  • Your script either didn't execute or didn't exit gracefully. I've updated my answer with a bit of code to see the output to help you diagnose what went wrong. If you still have problems, you should accept my answer, as it resolved your question, and ask a new question – Alastair McCormack Nov 27 '15 at 14:50
  • Thanks on your answer, but it's still not working. This is the error it's printing in exception `b''` – Nobbie Nov 27 '15 at 17:57
  • Try it with `stderr=subprocess.STDOUT` – Alastair McCormack Nov 27 '15 at 17:59
  • This fixed it by 50%. `b'Traceback (most recent call last):\r\n File "test_script.py", line 1, in \r\n a = int(input())\r\nEOFError: EOF when reading a line\r\n'` – Nobbie Nov 27 '15 at 18:17
  • See comments. You're really approaching this problem in the wrong way :( – Alastair McCormack Nov 27 '15 at 19:34
0

https://docs.python.org/3/library/subprocess.html#subprocess.check_output

import subprocess                                                                      
subprocess.check_output("python test_script.py", stderr=subprocess.STDOUT, shell=True)