欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > pyinstaller打包exe多种失败原因解决方法

pyinstaller打包exe多种失败原因解决方法

2025/2/23 1:44:30 来源:https://blog.csdn.net/OVEN_b612/article/details/139649852  浏览:    关键词:pyinstaller打包exe多种失败原因解决方法

pyinstaller打包exe多种失败原因解决方法

目录

  • pyinstaller打包exe多种失败原因解决方法
    • 1、pyinstaller安装有问题
      • 1.1 安装pyinstaller
      • 1.2 采用anconda的环境启动
    • 2、pyqt5与pyside6冲突
      • 2.1 打包生成.spec文件
      • 2.2 编辑spec文件
    • 3、打包成功后打不开exe,exe闪退
      • 3.1 spec文件添加对应库
      • 3.2 spec文件添加对应图片
      • 3.3 重新打包

在制作exe程序时,使用pyinstaller打包经常出现各种错误例如1、pyinstaller安装有问题;2、显示pyqt5与pyside6冲突;3、打包成功后打不开exe,exe闪退等。下面记录一下自己的解决方法,供参考。

1、pyinstaller安装有问题

1.1 安装pyinstaller

首先需要使用pip install pyinstaller对pyinstaller进行安装,最开始我以为我安装成功了因为结果显示
Requirement already satisfied: pyinstaller in…

但是我在使用打包语句(例如python -m PyInstaller --onefile --windowed simulation_app.py) 时一直显示
**‘pyinstaller’ 不是内部或外部命令,也不是可运行的程序或批处理文件。**无法继续下一步

1.2 采用anconda的环境启动

问了下gpt是因为太多的环境都有,系统不知道用哪个

于是我在anconda的环境中安装了pyinstaller,并在对应prompt中打开cmd界面
在这里插入图片描述

看了一下pyinstaller成功装上了,路径必须要有pyinstaller.exe程序。
在这里插入图片描述

在对应的环境启动prompt进行打包。
在这里插入图片描述

2、pyqt5与pyside6冲突

2.1 打包生成.spec文件

我先打包运行 pyinstaller --onefile --windowed --icon=app1_image1.ico app2.py
但是报错 **attempting to run hook for ‘PySide6’…**这个应该是PySide6与pyqt5冲突导致的
之后我把PySide6删了但是报错,没办法网上找了下资料,最后通过编辑spec文件解决

2.2 编辑spec文件

我是先把之前build和dist文件夹都先删除了,只剩下spec文件然后打开编辑。
在这里插入图片描述
找到**excludes=[],**修改为*excludes=[‘PySide6’],

a = Analysis(['app2.py'],pathex=[],binaries=[],datas=[],hiddenimports=[],hookspath=[],runtime_hooks=[],excludes=[],win_no_prefer_redirects=False,win_private_assemblies=False,cipher=block_cipher,
)

重新打包解决pyqt5与pyside6冲突问题了

3、打包成功后打不开exe,exe闪退

生成完后exe闪退,我这里的原因是使用了pyqt5的这些包,供参考

import sys
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QSpinBox, QFileDialog, QMessageBox, QStackedWidget, QSizePolicy
from PyQt5.QtGui import QIcon, QPixmap, QFont
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import time

使用 PyQt5 库时,通过 PyInstaller 打包可能会遇到一些特定的问题,例如需要手动指定一些隐藏的导入。下面是详细的步骤

3.1 spec文件添加对应库

找到hiddenimports并添加对应的库

a = Analysis(['simulation_app.py'],pathex=['.'],binaries=[],datas=[('chengzi.ico', '.')],hiddenimports=['PyQt5.QtWidgets','PyQt5.QtGui','PyQt5.QtCore','PyQt5.QtSvg','PyQt5.QtNetwork','PyQt5.QtPrintSupport','PyQt5.QtMultimedia','PyQt5.QtMultimediaWidgets','PyQt5.QtWebEngineWidgets','PyQt5.QtQuick','PyQt5.QtQml'],

3.2 spec文件添加对应图片

我的exe中还有图片,所以也要添加进spec文件找到datas并添加,参考如下格式

datas=[('app1_image1.png', '.'),  # 包含 app1_image1.png 文件('result.png', '.'),  # 包含 result.png 文件],

3.3 重新打包

显示成功
在这里插入图片描述
找到dist文件夹,exe文件成功生成,并且可以正确运行。
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词