使用shebang使用subprocess.call执行python脚本

Evp*_*pok 3 python subprocess shebang python-3.x

我正在用Python 3编写一个(某种程度上)模块化的应用程序,我想从它运行任意程序,所述程序在运行时指定,而不一定是python脚本.

所以我用例如,

subprocess.call([spam, "-i", eggs, "-o", ham])
Run Code Online (Sandbox Code Playgroud)

如果spam是一个python脚本,有shebang python3和可执行权限,我得到

OSError: [Errno 8] Exec format error
Run Code Online (Sandbox Code Playgroud)

如果我

subprocess.call(["python3", spam, "-i", eggs, "-o", ham])
Run Code Online (Sandbox Code Playgroud)

它工作正常.

你知道为什么吗?如何在spam不指定的情况下运行python3

Chr*_*rle 8

您需要使用shell=True,并且您需要将您的数组转换为命令字符串,如下所示:

subprocess.call(' '.join([spam, "-i", eggs, "-o", ham]), shell=True)
Run Code Online (Sandbox Code Playgroud)

这将调用shell而不是直接命令,shell应该能够处理shebang.