1

I'm running a subprocess in python with

data = subprocess.run(
    command, 
    shell=True,
    stderr=subprocess.STDOUT,
    text=True
)

however, some of these commands require input from the user. For example, if I were to run

data = subprocess.run(
    """python -c 'input("What is your input?")'""", 
    shell=True,
    stderr=subprocess.STDOUT,
    text=True
)

it would hang. For the commands that would be run, there is a way to not accept any input, however that may damage some of the internals since it then has to make assumptions (which are usually wrong). I would like to be able to stream the output to a function as it is generated, as well as be able to pipe in input as it is asked for within the subprocess's run cycle.

However, I've no clue how to do this. If someone could guide me in the right direction, I'd be eternally grateful!

  • `input` just reads a line from `stdin`, so you just need to set the subprocess' `stdin` to `subprocess.PIPE` and write to `data.stdin` via `data.communicate` (preferable) or `data.stdin.write`. – Paul M. Jun 28 '20 at 20:39
  • I believe [this](https://stackoverflow.com/q/62288531/9608759) is **highly** related. According to answers in that page, you can't use subprocess if you want to make an interactive application. – Asocia Jun 28 '20 at 20:41
  • 2
    Does this answer your question? [How to capture inputs and outputs of a child process?](https://stackoverflow.com/questions/62288531/how-to-capture-inputs-and-outputs-of-a-child-process) – AMC Jun 28 '20 at 20:48
  • ``subprocess.run`` takes an ``stdin`` parameter, which is data that get directly passed to the subprocess' stdin. Is there a reason why you do not use it? What other behaviour do you need? – MisterMiyagi Jun 28 '20 at 20:57

0 Answers0