简体   繁体   English

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

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

I am trying to call a python script from another using subprocess.Popen() .我正在尝试使用 subprocess.Popen subprocess.Popen()从另一个调用 python 脚本。 It works perfectly when I debug the program line-by-line, however when I run the program normally I get the following error:当我逐行调试程序时它工作得很好,但是当我正常运行程序时,我收到以下错误:

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

I'm stumped as to what the issue is as it works without fault when the program is being debugged line-by-line so not sure what changes exactly when the program is run normally.我对问题是什么感到困惑,因为当程序被逐行调试时它可以正常工作,所以不确定程序正常运行时究竟发生了什么变化。

I am trying to pass a set of arguments into the subprocesses, and am then parsing these using argparse in the child process.我正在尝试将一组 arguments 传递给子进程,然后在子进程中使用argparse解析这些。

This is how I am calling the process:这就是我调用该过程的方式:

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

And here is how I am parsing the arguments in the child process:这是我在子进程中解析 arguments 的方式:

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()

Can anyone spot what I'm missing?谁能发现我错过了什么?

I've managed to fix this issue by creating my command and arguments as a string first and then using shlex.Split() to split it into an arguments list.我设法通过首先创建我的命令和 arguments 作为字符串然后使用shlex.Split()将其拆分为 arguments 列表来解决此问题。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 在同一目录中打开文件的代码在运行时有效,但在 VSCode 中调试时无效 - Python code that opens a file in the same directory works while running but not when debugging in VSCode 使用子进程在Python中运行linux命令时出错 - Error while running linux command in Python using subprocess 在 CentOS6 中运行 python 子进程和 Libreoffice 6.2 时无法打开显示错误 - Failed to Open Display error while running python subprocess and Libreoffice 6.2 in CentOS6 在 macOS Monterey 12.4 上运行“where python”而“where python3”工作时找不到 Python - Python not found when running `where python` on macOS Monterey 12.4 while `where python3` works git 进程作为 Python 子进程运行时从不退出 - The git process never exits when running as a Python subprocess 执行 Python 搁置打开时出现 gdbm 错误 22 - gdbm error 22 when performing Python shelve open 运行python脚本时子进程总是自动启动 - Subprocess always automatically starts when running python script 在Python 3x中使用subprocess.getstatusoutput时出错 - Error while using subprocess.getstatusoutput in Python 3x 运行python脚本时出错 - error while running python script 适用于Python 2但不适用于Python 3的Python子进程PIPE - Python subprocess PIPE that works on Python 2 but not on Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM