0

I'm trying to pass arguments to my test_script.py but I'm getting the following error. I know this isn't the best way to do this but it's the only one that will work since I won't know what functions are in test_script.py. How can I pass arguments as stdin input?

test_script.py

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

print(a+b)

main_script.py

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

Error

b'Traceback (most recent call last):\r\n File "test_script.py", line 1, in <module>\r\n a = int(input())\r\nEOFError: EOF when reading a line\r\n'
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
Nobbie
  • 13
  • 2
  • 3
  • in `main_script` you are passing numbers as argument but in `test_script` you are trying to read them from `stdin` – Saeid Nov 28 '15 at 20:34
  • Yes, I know that, but I don't know how to fix it – Nobbie Nov 28 '15 at 20:52
  • what are you actually trying to do? – Padraic Cunningham Nov 28 '15 at 21:02
  • A script that test other scripts, you type input data and read the output, an if you know correct output you can see if the script is working properly – Nobbie Nov 28 '15 at 21:10
  • The dupe covers pretty much all you need – Padraic Cunningham Nov 28 '15 at 21:13
  • I saw that 3 days ago and it didn't help me – Nobbie Nov 28 '15 at 21:18
  • If it did not help then you must not have read the it because it does exactly what you want – Padraic Cunningham Nov 29 '15 at 01:52
  • ***(1)*** put the code from `test_script.py` into a function (the function is `func = lambda a, b: a + b` in this case) then import the module and call the function: `import the_module; print(the_module.func(2, 3))` (assuming `test_script.py` is renamed to `the_module.py`) -- [related question](http://stackoverflow.com/q/30076185/4279). OR ***(2)*** if you want to pass input to a subprocess (doesn't matter how it is implemented) via its stdin then see [Python - How do I pass a string into subprocess.Popen (using the stdin argument)?](http://stackoverflow.com/q/163542/4279) – jfs Nov 29 '15 at 08:52

3 Answers3

1

If do not want to use argv, however is odd, consider Popen and operating/communicating on stdin/stdout

from subprocess import Popen, PIPE, STDOUT

p = Popen(['python', 'test_script.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

p_stdout = p.communicate(input=b'1\n2\n')[0]
# python 2
# p_stdout = p.communicate(input='1\n2\n')[0]
print(p_stdout.decode('utf-8').strip())
# python2
# print(p_stdout)

As a reference from SO Python subprocess and user interaction.

And even more info on https://pymotw.com/2/subprocess/

Community
  • 1
  • 1
kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • I'm getting error: `p_stdout = p.communicate(input='1\n2\n')[0] File "C:\Python34\lib\subprocess.py", line 959, in communicate stdout, stderr = self._communicate(input, endtime, timeout) File "C:\Python34\lib\subprocess.py", line 1195, in _communicate self.stdin.write(input) TypeError: 'str' does not support the buffer interface` – Nobbie Nov 28 '15 at 21:16
  • edited to be compatibile with python3 – kwarunek Nov 28 '15 at 21:20
-1

Not sure what are you trying to do but here is one working example:

import sys

# print('Number of arguments:', len(sys.argv), 'arguments.')
# print('Argument List:', str(sys.argv))

# print(sys.argv[1])
# print(sys.argv[2])

a = int(sys.argv[1])
b = int(sys.argv[2])

print(a+b)

And your main_script.py:

import subprocess

try:

  out = subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT)
  print(out)

except subprocess.CalledProcessError as e:
  print(e.output)
sstevan
  • 477
  • 2
  • 9
  • 25
  • This works but it uses `sys.argv`, is there any way for this to work with `input()`, or to change `input()` to `sys.argv` – Nobbie Nov 28 '15 at 20:55
-1

That is jot going to work, test_script.py expects for keyboard input not the argument.

If you want main_script.py to pass arguments to test_script.py you have to amend test_script.py below code should do the trick

import sys

args = sys.argv[1:]
for arg in args:
    print arg

otherwise you can chek argparse https://docs.python.org/2/library/argparse.html

Arunas V.
  • 74
  • 3