【问题标题】:Subprocess "python file.py" – how to ensure runs in python3?子进程“python file.py”——如何确保在 python3 中运行?
【发布时间】:2016-02-24 23:11:17
【问题描述】:

我正在运行以下代码:

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

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

谢谢!

【问题讨论】:

  • 您可能想要更改代码以便它在两者上运行:p=subprocess.check_output('python ./file.py < input.txt', shell=True)

标签: python-2.7 python-3.x subprocess


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 1970-01-01
      • 2011-08-31
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      相关资源
      最近更新 更多