pyinstaller打包过程与问题总结
安装环境:
pycharm、Windows 10
打包步骤:
1.安装pyinstaller;
2.安装pywin32;
3.切换到打包的.py文件所在路径;
4.执行指令:pyinstaller -F XXX.py
打包测试的GitHub代码地址:测试pyinstaller打包
Pyinstaller常用参数说明:
–i 图标路径;
-F 打包成一个exe文件;
-w 无控制台窗口;
-c 使用控制台;
-D 创建一个目录,里面包含exe以及其他一些依赖性文件;
pyinstaller -h 来查看参数;
测试:
1. 打包至一个exe文件(无窗口 -w):
pyinstaller -F testApscheduler.py
生成结果:
2. exe添加icon (无窗口 -w):
pyinstaller -F -w -i=a.ico testApscheduler.py
生成结果:
注:
在替换icon的过程中,遇到一个莫名其妙的问题,记录如下。
在dist文件夹中,如果文件以详细信息展示,则图标正确显示:
但是如果以大缩略图的形式展现,则又变成了本身系统自带的图标,如下图:
将此exe复制到桌面上可以看到,显示的也是替换后的icon:
此问题有可能是显示的bug……
其余参数:
问题总结:
问题1.两个同名exe进程
Pyinstaller在-F单文件模式下打包执行后,在任务管理器中存在两个同名exe进程,如图:
两个进程分别是一个主进程,一个清理进程;
当关闭主进程时,两个进程同时消失;当只关闭清理进程时,主进程依然存在。
此问题在GitHub说明:https://github.com/pyinstaller/pyinstaller/issues/2483
问题2. -i 添加ioc报错问题
图标ico的问题
直接修改图片后缀,从png或者jpg直接修改成ico,导致ioc无法正常加载到程序中
问题3. No trigger by the name "cron" was found
打包exe,运行时报错:APScheduler: No trigger by the name "cron" was found
由于打包程序主要功能是执行定时加载某个方法,添加任务的方式在scheduler.add_job()方法中不能直接写成:scheduler.add_job(tick, 'cron', hour=19,minute=23);需要在CronTrigger包下新建trigger:trigger = CronTrigger(hour='15', minute='55'),再将此对象添加到add_job()方法中。
猜测的原因可能是打包之后,add_job()方法无法识别’cron’参数,需要采用CronTrigger()方式生成触发器对象,类似对参数包装一层生成对象入参。修改如下:
修改前:
scheduler.add_job(tick, 'cron', hour=19,minute=23)
修改后:
from apscheduler.triggers.cron import CronTrigger
trigger = CronTrigger(hour='15',minute='04')#设置参数
scheduler.add_job(tick, trigger=trigger)
参考链接:
https://www.jianshu.com/p/bf2e4e14d64a
https://blog.****.net/qq_35203425/article/details/78568141
https://www.cnblogs.com/dyzne/p/7169607.html