if __name__ == '__main__':
multiprocessing.freeze_support()
win = Window()
win.root.mainloop()
这里是官方github给出的解释:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing
解决方案:在你的主程序前添加一行代码
如果你的Pyinstaller版本低于3.3版本的话,还需要额外添加一个模块:
import os
import sys
# Module multiprocessing is organized differently in Python 3.4+
try:
# Python 3.4+
if sys.platform.startswith('win'):
import multiprocessing.popen_spawn_win32 as forking
else:
import multiprocessing.popen_fork as forking
except ImportError:
import multiprocessing.forking as forking
if sys.platform.startswith('win'):
# First define a modified version of Popen.
class _Popen(forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
os.putenv('_MEIPASS2', sys._MEIPASS)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
# Second override 'Popen' class with our modified version.
forking.Popen = _Popen
把上述内容保存为multiprocess.py ,然后在你的程序中引入该模块即可:
from multiprocess import *
部分来源于:https://blog.csdn.net/lghello/article/details/105824910
这篇博客介绍了如何处理使用PyInstaller打包Python程序时遇到的multiprocessing问题。通过添加特定代码片段,可以修复Windows平台上的错误。这些代码涉及修改Popen类以适应_frozen环境,并确保_MEIPASS2环境变量的正确设置。解决方案适用于PyInstaller低于3.3版本的情况。

1325

被折叠的 条评论
为什么被折叠?



