How to use subprocess to interact with a python script
Question: I'm writing an IDE for python, in python, and need to use subprocess to
intereact with a user's script.

I am completely new to using subprocess and not sure what I'm doing here. I've
created a test snippet representing what I'm trying to do:

    
    
    from subprocess import Popen,PIPE,STDOUT
    import tkinter as tk
    
    t=tk.Text()
    t.pack()
    
    p = Popen(["python","c:/runme.py"],stdout=PIPE,stdin=PIPE,stderr=PIPE,shell=True)
    p.stdin.write("5".encode())
    out=p.stdout.read()
    
    t.insert(1.0,out)
    

And here is the test script I'm trying to interact with:

    
    
    print("Hello World")
    inp=input("Enter a Number: ")
    print(inp)
    quit()
    

Unfortunately it just waiting (presumably) on line 2. How do I read what has
already been printed and how to I then input the string?


Answer: You have to flush stdout regularly, because, if the script is not connected to
a terminal, the output is not automatically flushed:

    
    
    import sys
    print("Hello World")
    print("Enter a Number: ")
    stdout.flush()
    inp = input()
    print(inp)
    

and you have to terminate the input by return `\n`:

    
    
    p = Popen(["python", "c:/runme.py"], stdout=PIPE, stdin=PIPE, stderr=PIPE)
    p.stdin.write("5\n".encode())
    out = p.stdout.read()
    

