pyinstaller打包单个exe后无法执行错误的解决方法
作者:RuiyuanZHOU 时间:2023-08-08 09:50:26
1、执行环境说明
python版本3.7
直接使用pip进行安装pywin32、pyinstaller
pip install pywin32
pip install pyinstaller
2、使用了第三方库的情况
建议在打包之前务必找到第三方库的包,把包复制到到跟myfile.py同目录下,然后再使用以上2种方式打包,否则会打包失败或者即使打包成功,程序也会闪退。pyinstaller -p参数是添加的pyinstaller打包程序时的扫描路径,假设venv\Lib\site-packages是包存放路径,那么也可以使用以下命令打包:
pyinstaller -p venv\Lib\site-packages -F xxx.py
3、failed to execute script 错误
首先使用
pyinstaller -F -w code.py
进行exe打包,得到的单个.exe文件,运行后提示failed to execute script 错误
排错过程:使用pyinstaller -D code.py进行exe打包,得到一个目录文件,通过命令行执行.exe文件提示The 'six' package is required; normally this is bundled with this package错误
说明pyinstaller打包后,需要six等库,最终确认需要在code.py中添加以下库:
import six
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements
当然,six和packaging库建议使用pip安装。加入以上库后,使用pyinstaller -D code.py打包执行不再出错。
4、找不到数据文件夹
有些程序包含了数据文件夹,不能直接按资源文件方式打包,需要在执行文件所在的文件夹内创建这些数据文件。一般情况下在脚本中我们可以使用os.path.split(os.path.abspath( __file__))来的到code.py的路径,然后拼接得到数据文件夹。但之后使用pyinstaller -F code.py打包成单个exe文件,在未读取数据文件夹的情况下运行正常,一旦打开数据文件,就会闪退,命令行窗口会显示打不开数据文件。因为PyInstaller会创建临时文件夹temp,程序代码在这个临时文件夹中运行,我们可以用以下几个语句来查看正式运行路径:
import sys
import os
print(sys.path[0])
print(sys.argv[0])
print(os.path.dirname(os.path.realpath(sys.executable)))
print(os.path.dirname(os.path.realpath(sys.argv[0])))
得到的结果是os.path.dirname(os.path.realpath(sys.executable))和os.path.dirname(os.path.realpath(sys.argv[0]))才是含数据文件夹的路径。因此可以按如下方式取得文件路径,然后根据需要拼接得到数据文件夹的真实路径:
if hasattr(sys, '_MEIPASS'):
# PyInstaller会创建临时文件夹temp
# 并把路径存储在_MEIPASS中
self.appPath = os.path.dirname(os.path.realpath(sys.executable))
else:
self.appPath, filename = os.path.split(os.path.abspath( __file__))
修改完成后,分别以以下三种方式打包,运行成功
pyinstaller -D code.py
pyinstaller -F code.py
pyinstaller -w -F code.py
来源:https://blog.csdn.net/zhoury/article/details/86104105
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
OpenCV图像处理之七种常用图像几何变换
![](https://img.aspxhome.com/file/2023/7/66427_0s.jpg)
SQLServer2005混合模式登录配置(用户登录错误18452,233,4064)
vuejs实现下拉框菜单选择
![](https://img.aspxhome.com/file/2023/9/56299_0s.jpg)
Yahoo发布一款FireFox网站开发插件
![](https://img.aspxhome.com/file/uploadpic/20079/23/2007923161456932.jpg)
12个步骤教你理解Python装饰器
python调用stitcher类自动实现多个图像拼接融合功能
![](https://img.aspxhome.com/file/2023/4/134804_0s.jpg)
nodeJS express路由学习req.body与req.query方法实例详解
![](https://img.aspxhome.com/file/2023/3/122383_0s.jpg)
asp如何制作一个小巧的购物车?
Python+Dlib+Opencv实现人脸采集并表情判别功能的代码
![](https://img.aspxhome.com/file/2023/4/117584_0s.jpg)
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
深入了解Python iter() 方法的用法
JavaScript中prototype为对象添加属性的误区介绍
![](https://img.aspxhome.com/file/2023/2/136332_0s.png)
如何基于Python实现电子邮件的发送
![](https://img.aspxhome.com/file/2023/3/69433_0s.png)
python 使用pygame工具包实现贪吃蛇游戏(多彩版)
![](https://img.aspxhome.com/file/2023/3/105383_0s.jpg)
5款Python程序员高频使用开发工具推荐
![](https://img.aspxhome.com/file/2023/5/81585_0s.jpg)
5个充满想象力的Web调色板
![](https://img.aspxhome.com/file/UploadPic/20088/2/200882125923403s.png)
Vue2.0 axios前后端登陆拦截器(实例讲解)
![](https://img.aspxhome.com/file/2023/9/139869_0s.jpg)
Python数据结构与算法中的栈详解(3)
![](https://img.aspxhome.com/file/2023/4/67384_0s.png)
python 美化输出信息的实例
![](https://img.aspxhome.com/file/2023/7/79757_0s.jpg)