子进程无法在非交互模式下运行python可执行文件?

子进程无法在非交互模式下运行python可执行文件?,python,shell,subprocess,interpreter,Python,Shell,Subprocess,Interpreter,我正在使用python 3.6.3和子流程模块运行另一个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()) 及 我希望它将运行并执行

我正在使用python 3.6.3和
子流程
模块运行另一个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
。但是,它会以交互模式打开python解释器

我测试了
shell=False
选项,它可以工作。我测试了字符串形式,而不是
args
的列表形式,它可以工作


我不确定它是否是一个bug。

您需要删除
shell=True
或将第一个参数更改为
executable+'test.py arg1'
而不是
[executable',test.py',arg1']


如中所述,对于
shell=True
,它将以
/bin/sh-c python test.py arg1
的形式运行,这意味着
python
将在没有参数的情况下运行。

这很可怕,并且会导致更复杂的参数出现问题。这是合理的,文档解释了它的工作原理。真正可能导致问题的是在不了解其工作原理的情况下使用某些东西。如果
stdin
连接到TTY,CPython将切换到交互模式。Popen的默认值是要继承的默认io句柄,它也将第二个解释器连接到您的TTY。尝试传递
stdin=None
@user2722968我无法在Python 3.6.5rc1中看到这种行为。如果我从终端(其中stdin是tty)运行
python3 test.py arg1
,它将运行脚本,而不会放到交互式shell中。
#  test.py

import sys
print(sys.argv)