本文介绍一个exe打包工具pyinstaller,使用非常简单、方便,而且还跨平台。
软硬件环境
- Windows 10 64bit
- PyQt5
- Anaconda with python 3.6.5
- pyinstaller
- apscheduler
- sqlalchemy
pyinstaller安装
命令行中执行
pip install pyinstaller
pyinstaller打包exe
示例还是以之前的为例,解压进入工程目录,命令行执行打包命令
pyinstaller -F --distpath release main.py
其中release是目标文件夹,main.py是工程入口文件,-F表示生成一个可执行文件。
pyinstaller的打包参数很多,需要的话通过help来查看
pyinstaller -h
subprocess打包问题
在打包包含subprocess.Popen时发现,加上参数--noconsole时产生的exe文件在运行的时候,进程并没有正确运行。比如需要利用subprocess.Popen创建一个进程去执行一个命令行操作,
mProcess = subprocess.Popen(cmd,stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)
pyinstaller打包操作命令如下
pyinstaller -F --noconsole --clean --distpath release main.py
打包后生成的exe,可以运行,不过查看进程并没有如预期正确地工作。
解决方法是在创建进程时,加上startupinfo参数,如下
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
mProcess = subprocess.Popen(cmd,stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True,startupinfo=si)
问题完美解决,具体可以参考文末的资料
apscheduler打包问题
项目中用到了apscheduler这个库,用来做定时任务非常的好,使用也很简单,不过在pyinstaller打包的时候,出现了如下错误
解决方法是,编辑文件/usr/local/lib/python3.5/dist-packages/apscheduler/__init__.py
# These will be removed in APScheduler 4.0.
# release = __import__('pkg_resources').get_distribution('APScheduler').version.split('-')[0]
# version_info = tuple(int(x) if x.isdigit() else x for x in release.split('.'))
# version = __version__ = '.'.join(str(x) for x in version_info[:3])
release = (3,3,1)"htmlcode">
# -*- coding: utf-8 -*- from PyInstaller.utils.hooks import copy_metadata datas = copy_metadata('apscheduler')最后执行打包命令
pyinstaller -F --clean --additional-hooks-dir hooks --distpath release main.pysqlalchemy打包问题
在用pyinstaller打包一个使用了sqlalchemy库的python3工程中,碰到了打包后的可执行文件报错的问题,如下图所示
解决方案是,将sqlalchemy.dialects中的mysql引入,然后采用mysql+pymysql的方式打开数据库,代码如下
import pymysql from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.dialects import mysql username = 'xugaoxiang' passwd = '123456' server = '192.168.0.100' port = '3306' dbname = 'djdb' cmd_connect = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(username, passwd, server, port, dbname) engine = create_engine(cmd_connect)执行pyinstaller打包命令
pyinstaller -F --clean --distpath shark main.py好了,至此pyinstaller打包python程序为exe安装文件的其实操作就算完成了,如果出现其它问题请直接联系我们,更多关于这方面的相关知识请查看下面的相关链接
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】