We have two python scripts, which need to communicate over stdin/out (on Windows). Sadly they both have to be in different Python versions. The snippets are:
Source (Python 3):
sys.stderr.write("LEN1: %s\n" % len(source_file.read()))
subprocess.check_call(["C:\\python2.exe", "-u", "-c",
"""
import foo
foo.to_json()
"""], stdin=source_file, stdout=json_file, stderr=sys.stderr, env=os.environ)
Target (Python 2):
def to_json():
import msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
input_message = sys.stdin.read()
sys.stderr.write("LEN2: %s\n" % len(input_message))
When I execute the scripts, I get:
LEN2: 0
LEN1: 37165
It seems like I am doing something fundamentally wrong, but cannot really figure out, what exactly. Could anyone try to help me debug, where I am going wrong.
Popenwith pipes and talking to it withcommunicate? docs.python.org/2/library/subprocess.html#subprocess.Popensetmodecall into_jsonis redundant since the-uargument setsstdinto binary mode. Passingenv=os.environis probably redundant, unlessos.environhas somehow gotten out of sync with the process environment.