pyinstaller打包exe后报fatal error return -1

来源:互联网 发布:买断软件源码 编辑:程序博客网 时间:2024/05/18 00:21

引起这个问题的原因,我在stackoverflow中找到了,如果调用控制台输入,输出,但是将python脚本打包成exe(非控制台 --window或--noconsole)的话,会出现这个问题。

解决方案是需要将输入、输出重定向。


参见stackoverflow

I had a similar issue with a root cause involving subprocess.Popen, if you are not using this function (or something similar) then this answer is unlikely to be helpful.

I was having a similar issue trying to build my .exe with pyinstaller. I was using the --noconsole flag as well as the --onefile flag with pyinstaller and was getting the "Fatal Error!" message " returned -1" whenever I tried to execute the resulting .exe file. My .exe would build work with just the --onefile flag but any combination using the --noconsole flag would return the error, leading me to believe it was the source of my issue.

After a little digging I tracked down this answer:

Python subprocess.call() fails when using pythonw.exe

Which seemed to indicate the issue was using pipes when using subprocess.Popen with pythonw.exe instead of python.exe. It seemed logical to me that pyinstaller with the --noconsole flag would use pythonw.exe instead of python.exe when building the .exe leading me to believe this could apply to my issue.

To test this I refactored by code to output the results of my Subprocess.Popen call to a file, then read in and deleted the file. This solved my "Fatal Error!" issue and everything else proceeded to work fine.

Original:

process = subprocess.Popen('cmd /c whoami', stdout=subprocess.PIPE)user = process.communicate()[0].decode('ascii').strip()

Refactored:

pro = subprocess.Popen('cmd /c whoami > "{0}"'.format(file_name))pro.wait()with open(file_name, 'rt') as file:    user = file.read().strip()os.remove(file_name)

2 0