我正在使用 python 3.6.3 和 subprocess 模块来运行另一个 python 脚本
# main.py
#!/bin/env python
from subprocess import Popen,PIPE
from sys import executable
p = Popen([executable, 'test.py', 'arg1'],shell=True, stdout=PIPE)
p.wait()
print(p.stdout.read().decode())
和
# test.py
import sys
print(sys.argv)
我希望它能运行并执行 test.py。但是,它会以交互模式打开 python 解释器!
我测试了 shell=False 选项,它有效。我测试了 args 的字符串形式而不是列表形式,它有效。
不知道是不是bug
请您参考如下方法:
您需要删除 shell=True 或将第一个参数更改为 executable + 'test.py arg1' 而不是 [executable, 'test. py', 'arg1'].
如 the documentation 中所述,使用 shell = True,它将以 /bin/sh -c python test.py arg1 运行它,这意味着 python 将运行没有争论。
