Pyinstallers打包深度学习模型采坑实录(Pyqt5+tensorflow1.13.1+keras2.2.4+python3.6.2+Anaconda)

1.基本步骤

  • (1)作者用Anaconda建立虚拟环境py36tf13,该环境下安装了Pyqt5+tensorflow1.13.1+keras2.2.4,pycharm平台进行调试,主程序名为spectrum.py,路径是cd /d E:\pycharmworkspace\Project32_MyUI\spectrum.py,所用到的UI和模型都位于spectrum.py同级目录下,可以完美运行后然后开始打包。
  • (2)打开Anaconda Prompt,使用cd /d E:\pycharmworkspace\Project32_MyUI定位到根目录下,然后输入pyinstaller -F -c spectrum.py,如果要去掉黑色命令框就用pyinstaller -F -w spectrum.py进行打包,打包过程可能会有一点点长,然后开始出现错误。
    2.出现错误
  • (1)首先是最大递归调用深度超出范围,需要进行修改。
RecursionError: maximum recursion depth exceeded

修改的方法如下,在生成的spectrum.spec开头加入两行代码(第二行就可以):

import sys  # 导入sys模块
sys.setrecursionlimit(3000)  # 将默认的递归深度修改为3000

这里参考下面的博客,非常感谢~
解决RecursionError: maximum recursion depth exceeded while calling a Python object
(2)解决之后,对spectrum.spec接着打包:pyinstaller -F -c spectrum.spec
然后又出现了另一个错误:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\HH\\AppData\\Local\\Temp\\_MEI180362\\astor\\VERSION'

需要在conda的虚拟环境中找到Pyinstaller,需要对其进行修改。
修改位置是D:\anaconda\envs\py36tf13\Lib\site-packages\PyInstaller\hooks,在该目录下创建hook-astor.py,里面内容是:

from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('astor')

然后继续打包pyinstaller -F -c spectrum.spec,就可以啦。
这里参考了
关于pyinstaller出现No such file or directory: 'C:\Users\qhcsu\AppData\Local\Temp\_MExx\xx\xx错误
3.其他方法
直接Win+R进行进入cmd命令框,cd到根目录下进行打包,打包完成后极有可能会无法打开exe,出现错误没有Keras或者Tensorflow的问题,但是如果已经安装过应该是没有问题的。

Logo

更多推荐