子进程 "python file.py" – 如何确保在 python3 中运行?

Subprocess "python file.py" – how to ensure runs in python3?

我是运行下面的一段代码:

p = subprocess.getoutput("python ./file.py")

如何确保使用的 python 版本是 python3?

谢谢!

假设您的控制脚本是 运行ning in python 3,因此您只是想 运行 相同版本的 python 的子进程,然后尝试:

import sys
p = subprocess.getoutput("'{}' ./file.py < input.txt".format(sys.executable))

确保使用 python 3.x 的明显方法是:

p = subprocess.getoutput("python3 ./file.py < input.txt")