Python 将变量传递给函数时子进程语法无效

Python Subprocess Invalid Syntax when passing variable to function

我有一个 python 脚本,想从中调用一个子进程。 以下示例完全正常工作:

脚本 1:

from subprocess import Popen

p = Popen('python Script2.py', shell=True)

脚本 2:

def execute():
    print('works!')

execute()

然而,当我想将变量传递给函数时,我收到以下错误:

def execute(random_variable: str):

SyntaxError: invalid syntax

脚本 1:

from subprocess import Popen 

p = Popen('python Script2.py', shell=True)

脚本 2:

def execute(random_variable: str):
    print(random_variable)

execute(random_variable='does not work')

有人知道为什么会这样吗?在网上找不到任何相关信息:(

Python 类型提示在 python3.5 中引入。 如果你 运行 这个版本低于 python3.5 那么它会抛出这个错误。

    def execute(random_variable: str):
                               ^
SyntaxError: invalid syntax

这就是为什么第一个脚本成功而后来失败的原因。

好的,伙计们,找到解决方案了:如果你在 mac...:[=​​11=],只需使用 'python3' 而不是 'python'

p = Popen('python3 Script2.py', shell=True)

感谢您的帮助! :)