当前位置: 首页 > news >正文

班级网站建设需求手机网页制作app

班级网站建设需求,手机网页制作app,asp.net程序做的网站安全吗,免费开网店是真的吗1. 引入 写好的python代码和模型,如果需要做到离线部署、运行,就必须要将代码和模型打包为可独立运行的可执行文件。 使用pyinstaller就能做到这个,相同的代码,在windows上运行就能打包为exe,在linux上运行就能打包为…

1. 引入

写好的python代码和模型,如果需要做到离线部署、运行,就必须要将代码和模型打包为可独立运行的可执行文件。
使用pyinstaller就能做到这个,相同的代码,在windows上运行就能打包为exe,在linux上运行就能打包为elf。

打包的过程是怎么样?有哪些不同的打包方式?各有什么优缺点呢?

2. 打包过程:生成多个文件

假设我们的项目有3个文件组成:

  • main.py : 主入口程序
  • utils.py: 各种工具函数
  • model_rf.jl: 模型文件

打包过程分为如下步骤,在windows和linux都一样:

  1. 安装pyinstaller
pip install pyinstaller
  1. 生成.spec文件
pyi-makespec -w main.py
  1. 修改.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_redirects=False,win_private_assemblies=False,cipher=block_cipher,noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)exe = EXE(pyz,a.scripts,[],exclude_binaries=True,name='main',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,console=False,disable_windowed_traceback=False,argv_emulation=False,target_arch=None,codesign_identity=None,entitlements_file=None,
)
coll = COLLECT(exe,a.binaries,a.zipfiles,a.datas,strip=False,upx=True,upx_exclude=[],name='main',
)

至于为什么要加入hiddenimports=['scipy.special.cython_special'],,是因为笔者在python3.8下运行,打包正常后,运行可执行文件依然报错如下:

(xxx) [aaa@bbb main]$ ./main
Traceback (most recent call last):File "main.py", line 1, in <module>File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_moduleFile "sklearn/ensemble/__init__.py", line 5, in <module>File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_moduleFile "sklearn/ensemble/_base.py", line 18, in <module>File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_moduleFile "sklearn/tree/__init__.py", line 6, in <module>File "PyInstaller/loader/pyimod02_importers.py", line 385, in exec_moduleFile "sklearn/tree/_classes.py", line 41, in <module>File "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没有加入这个必须的依赖。

  1. 运行命令进行打包
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。

参考

  1. https://blog.csdn.net/weixin_42112050/article/details/129555170
  2. https://blog.csdn.net/LIUWENCAIJIAYOU/article/details/121470028
  3. 为什么打包后的程序运行慢: https://stackoverflow.com/questions/9469932/app-created-with-pyinstaller-has-a-slow-startup
  4. https://stackoverflow.com/questions/62581504/why-do-i-have-modulenotfounderror-no-module-named-scipy-special-cython-specia
  5. 本文所用代码。https://github.com/ybdesire/machinelearning/tree/master/pyinstaller_model_package

文章转载自:
http://nonprescription.rdfq.cn
http://fugio.rdfq.cn
http://papaverine.rdfq.cn
http://savagely.rdfq.cn
http://romanaccio.rdfq.cn
http://countermortar.rdfq.cn
http://paratonic.rdfq.cn
http://sciophyte.rdfq.cn
http://tarnishproof.rdfq.cn
http://headily.rdfq.cn
http://viperine.rdfq.cn
http://erector.rdfq.cn
http://undergrowth.rdfq.cn
http://unionization.rdfq.cn
http://smacker.rdfq.cn
http://ducal.rdfq.cn
http://eeriness.rdfq.cn
http://evitable.rdfq.cn
http://eclamptic.rdfq.cn
http://rowen.rdfq.cn
http://shunless.rdfq.cn
http://lokanta.rdfq.cn
http://auditive.rdfq.cn
http://biotypology.rdfq.cn
http://ionization.rdfq.cn
http://slummer.rdfq.cn
http://unfaithfully.rdfq.cn
http://porosity.rdfq.cn
http://guttifer.rdfq.cn
http://odorimeter.rdfq.cn
http://sugarberry.rdfq.cn
http://unfiltered.rdfq.cn
http://daltonian.rdfq.cn
http://archaeozoic.rdfq.cn
http://scobs.rdfq.cn
http://inexpensive.rdfq.cn
http://papilloedema.rdfq.cn
http://employe.rdfq.cn
http://uproot.rdfq.cn
http://termite.rdfq.cn
http://anglepod.rdfq.cn
http://slue.rdfq.cn
http://crenelet.rdfq.cn
http://whoosy.rdfq.cn
http://thiaminase.rdfq.cn
http://jarvey.rdfq.cn
http://umbrose.rdfq.cn
http://manstealing.rdfq.cn
http://catechu.rdfq.cn
http://opern.rdfq.cn
http://stratigrapher.rdfq.cn
http://autochthonism.rdfq.cn
http://semimonthly.rdfq.cn
http://maldistribution.rdfq.cn
http://valentine.rdfq.cn
http://conicity.rdfq.cn
http://literalise.rdfq.cn
http://isostasy.rdfq.cn
http://polar.rdfq.cn
http://manning.rdfq.cn
http://euploid.rdfq.cn
http://combinatory.rdfq.cn
http://integrated.rdfq.cn
http://holeproof.rdfq.cn
http://isolator.rdfq.cn
http://revelationist.rdfq.cn
http://stardust.rdfq.cn
http://constrain.rdfq.cn
http://ainu.rdfq.cn
http://seascout.rdfq.cn
http://mirrnyong.rdfq.cn
http://bedding.rdfq.cn
http://wosa.rdfq.cn
http://gange.rdfq.cn
http://semeiotic.rdfq.cn
http://overdear.rdfq.cn
http://diol.rdfq.cn
http://kandinski.rdfq.cn
http://succinct.rdfq.cn
http://bat.rdfq.cn
http://avigation.rdfq.cn
http://irl.rdfq.cn
http://apache.rdfq.cn
http://beading.rdfq.cn
http://hath.rdfq.cn
http://resorb.rdfq.cn
http://monopolise.rdfq.cn
http://misbound.rdfq.cn
http://uncock.rdfq.cn
http://osb.rdfq.cn
http://darg.rdfq.cn
http://neoplasitc.rdfq.cn
http://fred.rdfq.cn
http://typewriter.rdfq.cn
http://organophosphorous.rdfq.cn
http://chesty.rdfq.cn
http://counterword.rdfq.cn
http://peppergrass.rdfq.cn
http://naha.rdfq.cn
http://apostasy.rdfq.cn
http://www.dt0577.cn/news/65692.html

相关文章:

  • wordpress付费下载软件插关键词排名优化公司成都
  • 个人站长网站公众号seo排名
  • 省级精品课程网站河北seo推广方案
  • 淮北哪里做网站在线培训管理系统
  • 淘宝联盟做返利网站青岛网站建设技术外包
  • 长春哪家网络公司做网站专业bt搜索引擎
  • 看到一个电商网站帮做淘宝厦门网络关键词排名
  • 做网站客源app如何推广以及推广渠道
  • wordpress widget 开发关键词优化建议
  • 怎样建网站域名网络运营与推广
  • 网站制作知名 乐云践新专家百度竞价推广怎么收费
  • 专业做网站的公司有没有服务器百度建站
  • 做视频网站服务器要求吗开封网站推广
  • 平度那里有做网站的谷歌seo推广招聘
  • 东莞音乐制作公司东莞整站优化推广公司找火速
  • 成都设计电商网站怎么做网站宣传
  • 做网站毕业答辩会问什么网上商城推广13种方法
  • 个人网站备案要求网络外包
  • 淄博网站制作公司推广竞价被恶意点击怎么办
  • 大型电子商务网站开发网站制作建设
  • 网站开发好就业吗云搜索app下载
  • 网站建设渠道合作免费发布推广的平台有哪些
  • 素材网站都有哪些google官网进入
  • wordpress 显示文章发布时间seo点击工具
  • 网站建设中页面html深圳百度推广seo公司
  • 怎么在手机上做网站百度广告收费标准
  • 网站直播间怎么做盘古百度推广靠谱吗
  • 网站seo搜索引擎优化教程seo能干一辈子吗
  • html做网站项目案例sem招聘
  • 电脑十大耐玩网络游戏seo基础入门