p = subprocess.Popen("python -i -u -B", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) print(p.stdout.readline()) print(p.stdout.readline()) print(p.stdout.readline()) print(p.stdout.readline()) p.stdin.write(b"print(2 ** 10);\n") # remove this later and try again p.stdin.flush() print("read result") print(p.stdout.readline()) p.terminate()