微网站制作,张家界公司网站建设,聚企网,wordpress忘记账号1. 引入
写好的python代码和模型#xff0c;如果需要做到离线部署、运行#xff0c;就必须要将代码和模型打包为可独立运行的可执行文件。 使用pyinstaller就能做到这个#xff0c;相同的代码#xff0c;在windows上运行就能打包为exe#xff0c;在linux上运行就能打包为…1. 引入
写好的python代码和模型如果需要做到离线部署、运行就必须要将代码和模型打包为可独立运行的可执行文件。 使用pyinstaller就能做到这个相同的代码在windows上运行就能打包为exe在linux上运行就能打包为elf。
打包的过程是怎么样有哪些不同的打包方式各有什么优缺点呢
2. 打包过程生成多个文件
假设我们的项目有3个文件组成
main.py : 主入口程序utils.py: 各种工具函数model_rf.jl: 模型文件
打包过程分为如下步骤在windows和linux都一样
安装pyinstaller
pip install pyinstaller生成.spec文件
pyi-makespec -w main.py修改.spec文件
注意几点 1主入口程序写在: Analysis第一个参数 2其他依赖程序写在Analysis第一个参数的列表中 3模型文件写在: binaries中注意要写为tuple
修改后好的.spec文件如下所示
# -*- mode: python ; coding: utf-8 -*-block_cipher Nonea Analysis([main.py,utils.py],pathex[],binaries[(model_rf.jl,.)],datas[],hiddenimports[scipy.special.cython_special],hookspath[],hooksconfig{},runtime_hooks[],excludes[],win_no_prefer_redirectsFalse,win_private_assembliesFalse,cipherblock_cipher,noarchiveFalse,
)
pyz PYZ(a.pure, a.zipped_data, cipherblock_cipher)exe EXE(pyz,a.scripts,[],exclude_binariesTrue,namemain,debugFalse,bootloader_ignore_signalsFalse,stripFalse,upxTrue,consoleFalse,disable_windowed_tracebackFalse,argv_emulationFalse,target_archNone,codesign_identityNone,entitlements_fileNone,
)
coll COLLECT(exe,a.binaries,a.zipfiles,a.datas,stripFalse,upxTrue,upx_exclude[],namemain,
)
至于为什么要加入hiddenimports[scipy.special.cython_special],是因为笔者在python3.8下运行打包正常后运行可执行文件依然报错如下
(xxx) [aaabbb main]$ ./main
Traceback (most recent call last):File main.py, line 1, in moduleFile PyInstaller/loader/pyimod02_importers.py, line 385, in exec_moduleFile sklearn/ensemble/__init__.py, line 5, in moduleFile PyInstaller/loader/pyimod02_importers.py, line 385, in exec_moduleFile sklearn/ensemble/_base.py, line 18, in moduleFile PyInstaller/loader/pyimod02_importers.py, line 385, in exec_moduleFile sklearn/tree/__init__.py, line 6, in moduleFile PyInstaller/loader/pyimod02_importers.py, line 385, in exec_moduleFile sklearn/tree/_classes.py, line 41, in moduleFile sklearn/tree/_criterion.pyx, line 1, in init sklearn.tree._criterion
ModuleNotFoundError: No module named scipy.special.cython_special
[45300] Failed to execute script main due to unhandled exception!根据参考4加入后就能修正该错误因为pyinstaller没有加入这个必须的依赖。
运行命令进行打包
pyinstaller main.spec这种打包方式会生成一个可执行文件位于dist文件夹中也会生成很多个运行该可执行文件所需的依赖库(dll, so)所以部署时需要将整个文件夹拷贝到目标机。
那么能不能只生成一个可执行文件不生成额外的依赖文件呢
3. 打包过程生成单个文件
如果只有一个py文件那么使用一条命令就能实现生成独立的可执行文件
pyinstaller -F main.py但是我们这个例子中是有多个文件的这就必须用下面的命令来打包
pyinstaller -F -w main.py -p utils.py -p model_rf.jl --hidden-import scipy.special.cython_special这样就能在dist文件夹中生成一个较大的可执行文件部署时只需要部署这一个文件就可以。
4. 两种打包方式的区别
上面讲解了生成多个文件和生成单个文件两种pyinstaller的打包方式。看上去生成单个文件方式更方便。
但是实际运行打包后的可执行文件就能发现 1生成单个文件最终只生成一个可执行文件比较简单但是运行很慢 2生成多个文件最终生成一堆文件但是其中的可执行文件运行会快很多笔者实测这种方式比单个文件快5倍
为什么生成单个文件会更慢呢从参考3可知
“one file” mode – this mode means that it has to unpack all of the libraries to a temporary directory before the app can start
因为这个很大的单个文件在运行主函数前会将所有依赖都释放到临时文件中再加载运行。这个释放文件的操作需要占用I/O而且每次启动程序都释放文件自然就拖慢了运行速度。
5. 总结
pyinstaller能实现将多个.py文件和其他模型文件打包为可离线运行不安装配置环境就能运行的可执行文件EXE或者ELF。 打包时建议按照生成多个文件的方式来打包这样程序运行起来会更快。 本文用到的所有代码和相关文件都放到这个repo了在linux下是正确运行的https://github.com/ybdesire/machinelearning/tree/master/pyinstaller_model_package。
参考
https://blog.csdn.net/weixin_42112050/article/details/129555170https://blog.csdn.net/LIUWENCAIJIAYOU/article/details/121470028为什么打包后的程序运行慢 https://stackoverflow.com/questions/9469932/app-created-with-pyinstaller-has-a-slow-startuphttps://stackoverflow.com/questions/62581504/why-do-i-have-modulenotfounderror-no-module-named-scipy-special-cython-specia本文所用代码。https://github.com/ybdesire/machinelearning/tree/master/pyinstaller_model_package