子进程不能 运行 python 在非交互模式下执行?

subprocess cannot run python executable in non-interactive mode?

我正在使用 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 将是 运行没有参数。