Example test file (print.py):
import sys
print('stdout')
print('stderr', file=sys.stderr)
Code for running it (x.py):
import subprocess
cmd = ['python', 'print.py']
print(subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT))
If I run print.py from the shell, it prints stdout first. However, If I run x.py, it prints stderr first. Is there any way for me to get the output in the correct order?