简体   繁体   中英

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

I am trying to call a python script from another using subprocess.Popen() . 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.

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM