PyQt5+pyqtgraph程序无法用PyInstaller生成*.exe文件求助
问题:PyInstaller打包PyQtGraph程序时触发IndexError
环境信息
- PyQt5==5.15.7
- pyqtgraph==0.12.3
- pyinstaller==5.5
- Windows系统
问题现象
直接运行脚本pyqtplot_test.py完全正常,但执行打包命令:
pyinstaller --onefile pyqtplot_test.py
时抛出错误:
IndexError: tuple index out of range
程序代码
# -*- coding: utf-8 -*- import sys import random from PyQt5 import QtWidgets, QtCore import pyqtgraph as pg backend_names = ['Qt5Agg'] class MyPlotWidget(QtWidgets.QWidget): UPDATE_TIME = 100 def __init__(self): super().__init__() self.graph_widget = pg.PlotWidget() self.graph_widget.setBackground("w") self.x_values = [0] self.y_values = [0] self.graph_widget.setXRange(self.x_values[0], self.x_values[-1] + 500, padding=0) self.graph_widget.setYRange(min(self.y_values) -100, max(self.y_values) +100, padding=0.1) styles = {'color':'k', 'font-size':'15px'} self.graph_widget.setLabel('left', '[dec]', **styles) self.graph_widget.setLabel('bottom', 'Time in [ms]', **styles) self.graph_widget.showGrid(x=True, y=True) self.graph_widget.addLegend() self.data_line1 = self.graph_widget.plot( self.x_values, self.y_values, name="Line", pen=pg.mkPen(color="k", width=2) ) # update timer self.timer = QtCore.QTimer() self.timer.setInterval( MyPlotWidget.UPDATE_TIME ) self.timer.timeout.connect(self._update_data) # user controls self.enable_button = QtWidgets.QPushButton("Enable") self.enable_button.clicked.connect(self._activation_button) # overall layout widget_layout = QtWidgets.QVBoxLayout() widget_layout.addWidget(self.graph_widget) widget_layout.addWidget(self.enable_button) self.setLayout(widget_layout) def _activation_button(self): if self.enable_button.text() == "Enable": self.enable_button.setText("Disable") self.enable_button.setStyleSheet("background-color: lightgreen") self.timer.start() elif self.enable_button.text() == "Disable": self.enable_button.setText("Enable") self.enable_button.setStyleSheet(None) self.timer.stop() def _update_data(self): # update the current data # self.y_values = self.y_values[1:] # remove the first element # append new values self.y_values.append( random.randint(-120,120) ) # add new data point # self.x_values = self.x_values[1:] # remove the first element self.x_values.append(self.x_values[-1] + MyPlotWidget.UPDATE_TIME) # new value 1 point higher than the last if len(self.x_values) > 20: start = self.x_values[-20] else: start = self.x_values[0] self.graph_widget.setXRange(start, self.x_values[-1] + 500, padding=0 ) # update the x drawing of window analog to incoming data self.data_line1.setData( self.x_values, self.y_values ) class MyTestMainWindow(QtWidgets.QMainWindow): def __init__(self): super(MyTestMainWindow, self).__init__() self._init_ui() def _init_ui(self): self.setWindowTitle("PLOTTING TESTER") self.setMinimumSize(640, 480) self.plot_widget = MyPlotWidget() self.setCentralWidget(self.plot_widget) self.show() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) _ = MyTestMainWindow() sys.exit(app.exec_())
解决方案
该错误源于PyInstaller打包时无法自动识别PyQtGraph的后端依赖,导致运行时检测逻辑异常。以下是三种可行解决方法:
方法1:打包命令添加隐藏导入
执行打包命令时显式指定PyQtGraph的后端模块:
pyinstaller --onefile --hidden-import=pyqtgraph.backends --hidden-import=pyqtgraph.backends.Qt5Agg pyqtplot_test.py
方法2:代码中强制指定后端
在导入PyQtGraph后立即设置后端,避免自动检测出错。修改代码开头部分:
import sys import random from PyQt5 import QtWidgets, QtCore import pyqtgraph as pg # 强制指定后端,替代原有的backend_names变量 pg.setConfigOption('backend', 'Qt5Agg')
方法3:通过.spec文件配置打包
- 先生成基础.spec文件:
pyinstaller --onefile pyqtplot_test.py
- 打开生成的
pyqtplot_test.spec,在hiddenimports列表中添加依赖:
hiddenimports=['pyqtgraph.backends', 'pyqtgraph.backends.Qt5Agg'],
- 用配置文件重新打包:
pyinstaller pyqtplot_test.spec
验证
打包完成后进入dist目录,运行生成的EXE文件,检查界面是否正常显示,点击Enable按钮后数据是否能实时更新。
内容的提问来源于stack exchange,提问作者user20261534
相关产品推荐
相关产品推荐

