2023/04/20
使用Pyinstaller打包exe总结
案例
Pyinstaller -F -w -i chengzi.ico py_word.py
-F 打包exe
-w 不带控制台的打包
-i img 打包指定exe图标打包
自己使用
指令1:
Pyinstaller -F -w -i Rpa_icon.bmp RPA_GYJ_SCENE_1.py
这个会将用到的包全部打包到一个可执行文件中,文件很大,exe执行效率比较低
指令2:
Pyinstaller RPA_GYJ_SCENE_1.py
会生成:RPA_GYJ_SCENE_1.spec
会报【问题1】的错误,按问题1的解决方案解决就行。
接着运行:Pyinstaller RPA_GYJ_SCENE_1.spec
问题
问题1 |
Explanation: Python's stack-limit is a safety-belt against endless recursion, eating up memory. PyInstaller imports modules recursively. If the structure how modules are imported within your program is awkward, this leads to the nesting being too deep and hitting Python's stack-limit. With the default recursion limit (1000), the recursion error occurs at about 115 nested imported, with limit 2000 at about 240, with limit 5000 at about 660. 【Python的堆栈限制是防止无休止递归的安全带,吞噬记忆。PyInstaller递归地导入模块。如果结构 在程序中导入模块的方式很尴尬,这会导致嵌套太深并且达到Python的堆栈限制。 对于默认的递归极限(1000),递归错误发生在115嵌套导入,限制2000为约240,限制5000为约660.】 |
1)在程序的RPA_GYJ_SCENE_1.spec文件中,在顶部附近添加以下行: import sys sys.setrecursionlimit(sys.getrecursionlimit()*5) 2)再执行 pyinstaller RPA_GYJ_SCENE_1.spec |
问题2 |
这部分实际上是警告,可以不用管 实际的问题是,定位字符识别模型和字符集文件问题,不知道为什么没有找到它默认的路径: D:\ProgramData\Anaconda3\Lib\site-packages\cnocr\label_cn.txt |
解决方案: 在代码里指定model 和 label_cn.txt的路径 然后在程序当前目录下创建对应的文件夹,并将模型拷贝到这个文件夹下: model\2.2\densenet_lite_136-fc\cnocr-v2.2-densenet_lite_136-fc-epoch=039-complete_match_epoch=0.8597-model.onnx 解决问题心得:可以使用工具,但不要太过依赖工具,要去分析问题的原因。 |
-------------------------------------------------------------------------------------------------------------------------
使用cx-freeze打包exe总结
参考:python- cx_Freeze安装、打包exe文件_python cx_freeze_kun_dl的博客-CSDN博客
pip install cx-freeze
如果当前环境配置的是Anaconda3 就在截图这个地方执行,这个地方和命令行的对话框是一样的 安装的东西会直接和当前配置的环境关联
安装后Anaconda3的script目录下会有三个文件
然后切换到这个目录下执行:python cxfreeze-postinstall
打包指令:
cxfreeze D:\Kaggle\company\Release_3\img_stitching
_para_test.py --target-dir D:\Kaggle\company\11
遇到的错误::
cx_freeze importError:numpy.core.umath to import
ImportError: numpy.core.multiarray failed to import
解决方法是更新numpy :pip install -U numpy,然后添加:
import numpy.core._methods
import numpy.lib.format