Pyinstaller 打包程序踩过的坑(1.Failed to execute script ;2.No module named typedefs等问题)

Pyinstaller 打包程序踩过的坑(1.Failed to execute script ;2.No module named typedefs;3 Could not find or load the Qt platform plugin “windows”;4 warning:lib not found)

最近写了一个程序,综合比较后选择PyInstaller打包程序(相比起来其他的打包工具真的安装繁琐,且出问题概率更高)。由于打包过程中急于求成遇到了不少问题,踩过的坑包括但不限于:

1.Failed to execute script

导致脚本无法执行的问题很多,如果是打包的GUI界面程序,很少会提示错在哪儿,仅仅显示‘Failed to execute script’。
Pyinstaller 打包程序踩过的坑(1.Failed to execute script ;2.No module named typedefs等问题)
建议对于任何程序的打包,都加上参数-c

pyinstaller -F -c -xxx.py

-c的作用是返回错误信息!如果有报错的话,将在控制台显示。这里,要做好截图的准备,因为控制台报错后是一闪而过的,如下。
Pyinstaller 打包程序踩过的坑(1.Failed to execute script ;2.No module named typedefs等问题)
对于pyinstaller,只要有报错信息就很容易解决,直接度娘搜答案即可。这里我的错误信息显示的是’No module named typedefs’,下边介绍解决方案。

2.No module named typedefs

这个问题很可能是因为程序里边有导入 sklearn 模块,为了进一步确认这个问题,可以在打包好的bulid 文件夹查看warning信息。
Pyinstaller 打包程序踩过的坑(1.Failed to execute script ;2.No module named typedefs等问题)
警告日志里边一般会有 sklearn 的关键字,确认问题。接下来就是删除原来的dist、build 文件、 spec文件, 在编译的时候加上:

pyinstaller -F -c xxx.py --hidden-import sklearn.neighbors.typedefs

继续打包即可。如果打包过程中出现最大递归深度的报错’Maximum recursion depth error’,在生成的xxx.spec文件中加入

import sys
sys.setrecursionlimit(5000)

同时把xxx.spec文件中

hiddenimports=[] 

修改为

hiddenimports=['cython','sklearn','sklearn.ensemble','sklearn.neighbors.typedefs','sklearn.neighbors.quad_tree','sklearn.tree._utils','scipy._lib.messagestream'] 

保存后运行即可

pyinstaller xxx.spec

3 Could not find or load the Qt platform plugin “windows”

如果打包PyQt做的GUI界面就容易出现这个问题,解决办法很简单,在电脑环境变量里边加上如下变量
变量名:QT_PATH
变量值:qt plugins的路径,如我的路径 ‘E:\Anaconda3\pkgs\qt-5.9.6-vc14h1e9a669_2\Library\plugins’
或者将该文件夹考至dist文件夹下(生成exe的文件夹)

4 warning:lib not found

如果在打包过程中,出现了很多’warning:lib not found’,这些警告暂时可以不用管,它列出了PyInstaller无法找到的模块, 并不意味着运行程序一定需要此模块,这个在警告日志里边也有解释。如果最后程序确实因为这些问题无法运行,可以把warning中这些dll的路径添加到环境变量中(一般这些dll的路径都在一个文件夹里边,搜索找到这个文件夹,复制路径添加到环境变量里),再次打包运行即可。

参考:

[1]:解决pyinstaller打包sklearn等库出现的问题: 提示failed to execute script xxx
https://blog.csdn.net/qq_40587575/article/details/86500445
[2]: Pyinstaller 打包遇到的一系列问题的解决方案
https://blog.csdn.net/windows_peng/article/details/81285658?utm_source=blogxgwz8
[3]: pyinstaller 打包成exe出现的问题+解决办法
https://segmentfault.com/a/1190000014781519