神剑山庄资源网 Design By www.hcban.com
基础环境
- python 2.7.17
- pyinstaller 3.5
安装pyinstaller
pip install pyinstaller
坑,大坑,深坑
背景:用pygame写了个贪吃蛇游戏,要打包成exe
用到了字体文件 C:\Windows\Fonts\simsun.ttc (宋体)
打包过程中没有报错
打包过程中的警告可以忽略,这个警告: WARNING: Hidden import “pygame._view” not found!
运行exe的时候报NotImplementedError: Can't perform this operation for unregistered loader type
真的是百思不得其姐,为什么会报这个错????
最终确定,是找不到引用的字体文件,需要指定下,添加如下代码:
def rp(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path)
并且每个文件都要使用该函数转换下地址
BASICFONT = pygame.font.Font(rp('C:\Windows\Fonts\simsun.ttc'), 18) titleFont = pygame.font.Font(rp('C:\Windows\Fonts\simsun.ttc'), 100) gameOverFont = pygame.font.Font(rp('freesansbold.ttf'), 100)
再次pyinstaller -F xxx.py
生成单个exe后,就可以直接运行不会报错了
上边解决了可能是巧合,因为每个人电脑上都有这个字体
再来个图片的,其他电脑上就没有了
首先,还是那个函数需要加到代码里
def rp(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path)
再者,把src目录下的background.jpg用上方的函数转换下地址,同时打印下地址以观后效
bgimg = rp(os.path.join('src','background.jpg')) print(bgimg)
使用 pyi-makespec -F 2048.py命令生成spec文件,修改文件内容如下:
指定src目录打包到exe中,运行时生成的临时路径也叫src
指定命令打包:pyinstaller -F 2048.spec
把2048.exe挪到另一个位置,跑一下看看cmd输出
生成的临时路径也叫src,且能找到我们的图片。
这时候还是不确定,我们换台机器跑下试试
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无pyinstaller打包程序exe踩过的坑的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.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】