首页 > 解决方案 > 将变量传递给函数时Python子进程无效语法

问题描述

我有一个 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')

有谁知道为什么会这样?在网上找不到任何关于它的东西:(

标签: pythonsubprocesssyntax-errorpopen

解决方案


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

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

感谢您的帮助!:)


推荐阅读