使用pyinstaller将python程序打包,不使用-w参数时(如“pyinstaller -F main.py -i cat.ico”)程序运行正常,但使用-w参数去掉console后,程序便卡在了一个地方无法继续运行,经过调试发现,出问题的地方在subprocess.Popen语句,原来是这样的:
popen = subprocess.Popen(cmd_list, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while popen.poll() is None:
line = popen.stdout.readline().decode('utf-8').strip()
print(line)
将subprocess.Popen的参数设为“shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT”便能正常运行了。将shell设为True后,通过shell执行指定的命令(后台应该默默地创建了一个console?),并利用创建的管道进行输入和输出。
我还试了一下,若将subprocess.Popen的参数设为“shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT”,则打包运行程序后会弹出来一个console,也就是说“stdin=subprocess.PIPE”是一