from subprocess import Popen, PIPE, STDOUT import sys python3 = sys.version_info[0] >= 3 def myprint(*args): sys.stdout.write(" ".join((str(x) for x in args)) + "\n") def myprint_noeol(*args): sys.stdout.write(" ".join((str(x) for x in args))) def test(shell=False, array=False): myprint("Testing with shell=", shell, "and array=", array) if shell and array: myprint(" ** This is the one that runs an interactive shell.") myprint(" ** You should press Ctrl-D.") cmdline = [ sys.executable, '-V' ] if not array: cmdline = " ".join(cmdline) p = Popen(cmdline, shell=shell, stdout=PIPE, stderr=STDOUT) out, err = p.communicate() out = out.strip() if python3: out = out.decode() myprint("\tOutput:") for line in out.split("\n"): myprint("\t[]", line) for shell in (True, False): for array in (True, False): test(shell=shell, array=array)