Python 子进程错误 22 - 在调试时有效但在 运行 时无效

Python subprocess Error 22 - Works while debugging but not when running

我正在尝试使用 subprocess.Popen() 从另一个脚本调用 python 脚本。当我逐行调试程序时,它工作得很好,但是当我 运行 程序正常时,我得到以下错误:

C:\Python38\python.exe: can't open file '"<filepath>\thumbs.py"': [Errno 22] Invalid argument

我对问题是什么感到困惑,因为当程序被逐行调试时它可以正常工作,所以不确定当程序 运行 正常时到底发生了什么变化。

我试图将一组参数传递给子进程,然后在子进程中使用 argparse 解析这些参数。

我是这样调用流程的:

cmd = ['python', "\"" + pythonPath + "thumbs.py\"", '-d', "\"" + outputdb + "\"", '-c', "\"" + path + cache + "\""]
subprocess.Popen(cmd).wait()

下面是我在子进程中解析参数的方式:

if __name__ == '__main__':
    global session
    args = None
    parser = argparse.ArgumentParser()

    parser.add_argument("-d", "--database", action="store", dest="dbname", help="Database to output results to", required=True)

    parser.add_argument("-c", "--cache", action="store", dest="cachefile", help="Cache file to scrape.", required=True)
    
    while args is None:
        args = parser.parse_args()

谁能看出我遗漏了什么?

我设法解决了这个问题,方法是先将命令和参数创建为字符串,然后使用 shlex.Split() 将其拆分为参数列表。

cmdStr = "python \"" + pythonPath + "thumbs.py\" -d \"" + outputdb + "\" -c \"" + path + cache + "\""
cmd = shlex.split(cmdStr)
subprocess.Popen(cmd).wait()

这里有更多信息:https://docs.python.org/3.4/library/subprocess.html#popen-constructor