'使用pyins构建发行版后,发生matplotlib qt5agg后台分段错误'

2024-06-05 19:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyqt5构建一个可分发的GUI,其结果是在弹出窗口中显示一个matplotlib图。它作为python代码运行良好,但是在通过pyinstaller将其构建为二进制之后,当单击用于创建包含matplotlib的弹出窗口的按钮时,它会抛出一个segmentation fault。你知道吗

它在Linux上运行,带有Python 3.7Pyinstaller 3.5。除绘图部分外,其他所有函数都正常工作,经过几次尝试,我发现弹出窗口生成工作正常,并且在调用FigureCanvasQt5Agg函数时会引发分段错误。我尝试使用gdb/faulthandler来跟踪故障,但无法跟踪,因为故障只发生在可分发的(如果我错了,请纠正我)。你知道吗

下面是代码:

from PyQt5 import QtCore, QtGui, QtWidgets

import matplotlib

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class Ui_PMI_init(object):
    def setupUi(self, PMI_init):
        PMI_init.setObjectName("PMI_init")
        PMI_init.resize(1113, 500)
        self.centralwidget = QtWidgets.QWidget(PMI_init)
        self.centralwidget.setObjectName("centralwidget")


        self.btn_resultwin = QtWidgets.QPushButton(self.centralwidget)
        self.btn_resultwin.setObjectName("btn_resultwin")
        self.btn_resultwin.clicked.connect(self.resultWindow)


        PMI_init.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(PMI_init)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1113, 21))
        self.menubar.setObjectName("menubar")
        PMI_init.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(PMI_init)
        self.statusbar.setObjectName("statusbar")
        PMI_init.setStatusBar(self.statusbar)

        # Edit layout of the widgets
        self.vbox_whole = QtWidgets.QVBoxLayout()
        self.vbox_whole.addWidget(self.btn_resultwin)

        self.centralwidget.setLayout(self.vbox_whole)

        self.retranslateUi(PMI_init)
        QtCore.QMetaObject.connectSlotsByName(PMI_init)



    def retranslateUi(self, PMI_init):
        _translate = QtCore.QCoreApplication.translate
        PMI_init.setWindowTitle(_translate("PMI_init", "PMI Input Information"))

        self.btn_resultwin.setText(_translate("PMI_init", "Show Results"))



    def resultWindow(self,PMI_init):
        self.winTable = showResultWindow()


        self.winTable.show()



class showResultWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.title = "Result Summary"
        self.top    = 150
        self.left   = 300
        self.width  = 870
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height) 

        self.newUI()

    def newUI(self):        

        self.fig = matplotlib.figure.Figure()
        self.canvas = FigureCanvas(self.fig)
        self.plot_layout = QtWidgets.QHBoxLayout()
        self.plot_layout.addWidget(self.canvas)

        self.result_grid = QtWidgets.QGridLayout()
        self.table_rlabel1 = QtWidgets.QLabel()
        self.table_rlabel2 = QtWidgets.QLabel()
        self.table_rlabel3 = QtWidgets.QLabel()
        self.table_rlabel4 = QtWidgets.QLabel()
        self.table_rlabel5 = QtWidgets.QLabel()

        self.table_rlabel1.setText('new') 
        self.table_rlabel2.setText('sum') 
        self.table_rlabel3.setText('new all') 
        self.table_rlabel4.setText('all') 
        self.table_rlabel5.setText('(%)') 

        self.result_grid.addWidget(self.table_rlabel1,0,1)
        self.result_grid.addWidget(self.table_rlabel2,0,2)
        self.result_grid.addWidget(self.table_rlabel3,0,3)
        self.result_grid.addWidget(self.table_rlabel4,0,4)
        self.result_grid.addWidget(self.table_rlabel5,0,5)

        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addLayout(self.plot_layout)
        self.vbox.addLayout(self.result_grid)
        self.vbox.setStretchFactor(self.plot_layout,1)
        self.vbox.setStretchFactor(self.result_grid,0)


        self.setLayout(self.vbox)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    PMI_init = QtWidgets.QMainWindow()
    ui = Ui_PMI_init()
    ui.setupUi(PMI_init)
    PMI_init.show()
    sys.exit(app.exec_())

还共享pyinstaller日志,因为发生了一些警告和“排除”,这可能会影响当前状态。你知道吗

206 INFO: PyInstaller: 3.5
206 INFO: Python: 3.7.3
208 INFO: Platform: Linux-2.6.32-696.el6.x86_64-x86_64-with-redhat-6.6-Santiago
212 INFO: wrote /user/neonbadak/002.GUIDevelopment/code/segfault_strippedcode.spec
219 INFO: UPX is not available.
226 INFO: Extending PYTHONPATH with paths
['/user/neonbadak/002.GUIDevelopment/code',
 '/user/neonbadak/002.GUIDevelopment/code']
226 INFO: checking Analysis
321 INFO: Building because /user/neonbadak/002.GUIDevelopment/code/segfault_strippedcode.py changed
322 INFO: Initializing module dependency graph...
342 INFO: Initializing module graph hooks...
349 INFO: Analyzing base_library.zip ...
7839 INFO: running Analysis Analysis-00.toc
7858 INFO: Caching module hooks...
7865 INFO: Analyzing /user/neonbadak/002.GUIDevelopment/code/segfault_strippedcode.py
8433 INFO: Processing pre-find module path hook   distutils
12072 INFO: Processing pre-safe import module hook   six.moves
17828 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
19177 INFO: Processing pre-find module path hook   site
19181 INFO: site: retargeting to fake-dir '/user/neonbadak/anaconda3/lib/python3.7/site-packages/PyInstaller-3.5-py3.7.egg/PyInstaller/fake-modules'
49655 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
92475 INFO: Loading module hooks...
92476 INFO: Loading module hook "hook-sphinx.py"...
/user/neonbadak/anaconda3/lib/python3.7/site-packages/sphinx/websupport/__init__.py:25: RemovedInSphinx20Warning: sphinx.websupport module is now provided as sphinxcontrib-websupport. sphinx.websupport will be removed at Sphinx-2.0. Please use the package instead.
  RemovedInSphinx20Warning)
102618 INFO: Loading module hook "hook-xml.dom.domreg.py"...
102621 INFO: Loading module hook "hook-sqlalchemy.py"...
103064 INFO:   Found 4 sqlalchemy hidden imports
103066 WARNING: Hidden import "MySQLdb" not found!
103067 WARNING: Hidden import "sqlalchemy.sql.functions.func" not found!
103101 INFO: Excluding import 'sqlalchemy.testing'
103112 INFO:   Removing import of sqlalchemy.testing.util from module sqlalchemy.orm.util
103113 INFO:   Removing import of sqlalchemy.testing.util from module sqlalchemy.testing
103113 INFO:   Removing import of sqlalchemy.testing.mock from module sqlalchemy.testing
103113 INFO:   Removing import of sqlalchemy.testing.exclusions from module sqlalchemy.testing
103113 INFO:   Removing import of sqlalchemy.testing.assertions from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing.assertsql from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing.config from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing.engines from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing.warnings from module sqlalchemy.testing
103114 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.config
103114 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.mock
103115 INFO:   Removing import of sqlalchemy.testing.util from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing.mock from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing.exclusions from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing.assertsql from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing.config from module sqlalchemy.testing.assertions
103115 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.assertsql
103116 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.util
103116 INFO:   Removing import of sqlalchemy.testing.config from module sqlalchemy.testing.util
103116 INFO:   Removing import of sqlalchemy.testing.engines from module sqlalchemy.testing.util
103116 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.engines
103116 INFO:   Removing import of sqlalchemy.testing.util from module sqlalchemy.testing.engines
103116 INFO:   Removing import of sqlalchemy.testing.config from module sqlalchemy.testing.engines
103117 INFO:   Removing import of sqlalchemy.testing.assertions from module sqlalchemy.testing.engines
103120 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.exclusions
103120 INFO:   Removing import of sqlalchemy.testing.config from module sqlalchemy.testing.exclusions
103120 INFO:   Removing import of sqlalchemy.testing from module sqlalchemy.testing.warnings
103120 INFO:   Removing import of sqlalchemy.testing.assertions from module sqlalchemy.testing.warnings
103121 INFO: Loading module hook "hook-nbformat.py"...
103152 INFO: Loading module hook "hook-sqlite3.py"...
103265 INFO: Loading module hook "hook-IPython.py"...
103323 INFO: Excluding import 'gtk'
103332 INFO: Excluding import 'PyQt4'
103339 INFO:   Removing import of PyQt4 from module IPython.external.qt_loaders
103340 INFO: Excluding import 'PyQt5'
103346 INFO:   Removing import of PyQt5.QtCore from module IPython.external.qt_loaders
103346 INFO:   Removing import of PyQt5 from module IPython.external.qt_loaders
103346 INFO:   Removing import of PyQt5.QtSvg from module IPython.external.qt_loaders
103346 INFO:   Removing import of PyQt5.QtGui from module IPython.external.qt_loaders
103347 INFO:   Removing import of PyQt5.QtWidgets from module IPython.external.qt_loaders
103347 INFO: Excluding import 'PySide'
103353 INFO:   Removing import of PySide from module IPython.external.qt_loaders
103354 INFO: Excluding import 'matplotlib'
103360 INFO:   Removing import of matplotlib from module IPython.core.pylabtools
103360 INFO:   Removing import of matplotlib.figure from module IPython.core.pylabtools
103360 INFO:   Removing import of matplotlib._pylab_helpers from module IPython.core.pylabtools
103360 INFO:   Removing import of matplotlib.pyplot from module IPython.core.pylabtools
103362 INFO: Loading module hook "hook-numpy.py"...
103364 INFO: Loading module hook "hook-PyQt5.py"...
120445 WARNING: Hidden import "PyQt5.sip" not found!
120445 INFO: Loading module hook "hook-matplotlib.py"...
121063 INFO: Loading module hook "hook-notebook.py"...
124324 INFO: Loading module hook "hook-PyQt5.QtSvg.py"...
124614 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
124670 INFO: Loading module hook "hook-lxml.etree.py"...
124673 INFO: Loading module hook "hook-PIL.py"...
124681 INFO: Excluding import 'tkinter'
124684 INFO: Import to be excluded not found: 'FixTk'
124684 INFO: Excluding import 'PyQt4'
124687 INFO:   Removing import of PyQt4 from module PIL.ImageQt
124687 INFO: Excluding import 'PyQt5'
124690 INFO:   Removing import of PyQt5.QtGui from module PIL.ImageQt
124690 INFO:   Removing import of PyQt5.QtCore from module PIL.ImageQt
124690 INFO: Excluding import 'PySide'
124693 INFO:   Removing import of PySide from module PIL.ImageQt
124693 INFO: Loading module hook "hook-pydoc.py"...
124695 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
124696 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
124900 INFO: Loading module hook "hook-lib2to3.py"...
124919 INFO: Loading module hook "hook-nbconvert.py"...
124986 INFO: Loading module hook "hook-pytz.py"...
125226 INFO: Loading module hook "hook-xml.py"...
125228 INFO: Loading module hook "hook-pygments.py"...
127436 INFO: Loading module hook "hook-matplotlib.backends.py"...
128523 INFO:   Matplotlib backend "GTK3Agg": ignored
    cairo backend requires that cairocffi or pycairo is installed
129194 INFO:   Matplotlib backend "GTK3Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
129906 INFO:   Matplotlib backend "MacOSX": ignored
    cannot import name '_macosx' from 'matplotlib.backends' (/user/neonbadak/anaconda3/lib/python3.7/site-packages/matplotlib/backends/__init__.py)
132630 INFO:   Matplotlib backend "nbAgg": added
133658 INFO:   Matplotlib backend "Qt4Agg": added
134318 INFO:   Matplotlib backend "Qt4Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
135049 INFO:   Matplotlib backend "Qt5Agg": added
135706 INFO:   Matplotlib backend "Qt5Cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
136394 INFO:   Matplotlib backend "TkAgg": added
137189 INFO:   Matplotlib backend "TkCairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
137832 INFO:   Matplotlib backend "WebAgg": added
138709 INFO:   Matplotlib backend "WX": ignored
    No module named 'wx'
139170 INFO:   Matplotlib backend "WXAgg": ignored
    No module named 'wx'
139827 INFO:   Matplotlib backend "WXCairo": ignored
    No module named 'wx'
140282 INFO:   Matplotlib backend "agg": added
141105 INFO:   Matplotlib backend "cairo": ignored
    cairo backend requires that cairocffi or pycairo is installed
141834 INFO:   Matplotlib backend "pdf": added
142716 INFO:   Matplotlib backend "pgf": added
143179 INFO:   Matplotlib backend "ps": added
143851 INFO:   Matplotlib backend "svg": added
144603 INFO:   Matplotlib backend "template": added
145066 INFO: Loading module hook "hook-pycparser.py"...
145069 INFO: Loading module hook "hook-sysconfig.py"...
145085 INFO: Loading module hook "hook-babel.py"...
145349 INFO: Loading module hook "hook-zmq.py"...
147784 INFO: Loading module hook "hook-cryptography.py"...
148313 INFO: Loading module hook "hook-distutils.py"...
148317 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
148445 INFO: Loading module hook "hook-certifi.py"...
148449 INFO: Loading module hook "hook-scipy.py"...
148452 INFO: Loading module hook "hook-jsonschema.py"...
148465 INFO: Loading module hook "hook-pkg_resources.py"...
149651 INFO: Processing pre-safe import module hook   win32com
149932 INFO: Loading module hook "hook-_tkinter.py"...
150165 INFO: checking Tree
150243 INFO: checking Tree
150301 INFO: Loading module hook "hook-pytest.py"...
152414 INFO: Loading module hook "hook-setuptools.py"...
153990 INFO: Loading module hook "hook-docutils.py"...
156694 INFO: Loading module hook "hook-jedi.py"...
156758 INFO: Loading module hook "hook-encodings.py"...
156917 INFO: Loading module hook "hook-PIL.Image.py"...
157844 INFO: Loading module hook "hook-jinja2.py"...
157868 INFO: Loading module hook "hook-numpy.core.py"...
158059 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
158072 INFO: Loading module hook "hook-gevent.py"...
158411 INFO: Determining a mapping of distributions to packages...
368388 WARNING: Unable to find package for requirement greenlet from package gevent.
368388 INFO: Packages required by gevent:
[]
371351 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
371359 INFO: Excluding import 'tkinter'
371362 INFO: Import to be excluded not found: 'FixTk'
371489 INFO: checking Tree
371811 INFO: Looking for ctypes DLLs
372140 WARNING: library user32 required via ctypes not found
373074 INFO: Analyzing run-time hooks ...
373117 INFO: Including run-time hook 'pyi_rth_traitlets.py'
373124 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
373130 INFO: Including run-time hook 'pyi_rth__tkinter.py'
373133 INFO: Including run-time hook 'pyi_rth_pkgres.py'
373136 INFO: Including run-time hook 'pyi_rth_certifi.py'
373139 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
373141 INFO: Including run-time hook 'pyi_rth_mpldata.py'
373143 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
373208 INFO: Looking for dynamic libraries
ldd: Warning: (sth written in Korean) `/user/neonbadak/anaconda3/plugins/platforms/../../lib/libgcc_s.so.1'
378343 INFO: Looking for eggs
378343 INFO: Python library not in binary dependencies. Doing additional searching...
378366 INFO: Using Python library /user/neonbadak/anaconda3/lib/libpython3.7m.so
378423 INFO: Warnings written to /user/neonbadak/002.GUIDevelopment/code/build/segfault_strippedcode/warn-segfault_strippedcode.txt
378773 INFO: Graph cross-reference written to /user/neonbadak/002.GUIDevelopment/code/build/segfault_strippedcode/xref-segfault_strippedcode.html
379184 INFO: checking PYZ
379213 INFO: Building because toc changed
379213 INFO: Building PYZ (ZlibArchive) /user/neonbadak/002.GUIDevelopment/code/build/segfault_strippedcode/PYZ-00.pyz
383447 INFO: Building PYZ (ZlibArchive) /user/neonbadak/002.GUIDevelopment/code/build/segfault_strippedcode/PYZ-00.pyz completed successfully.
383576 INFO: checking PKG
383577 INFO: Building because toc changed
383577 INFO: Building PKG (CArchive) PKG-00.pkg
383699 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
383703 INFO: Bootloader /user/neonbadak/anaconda3/lib/python3.7/site-packages/PyInstaller-3.5-py3.7.egg/PyInstaller/bootloader/Linux-64bit/run
383703 INFO: checking EXE
383705 INFO: Building because toc changed
383705 INFO: Building EXE from EXE-00.toc
383720 INFO: Appending archive to ELF section in EXE /user/neonbadak/002.GUIDevelopment/code/build/segfault_strippedcode/segfault_strippedcode
383911 INFO: Building EXE from EXE-00.toc completed successfully.
383951 INFO: checking COLLECT
384002 INFO: Building COLLECT COLLECT-00.toc
433891 INFO: Building COLLECT COLLECT-00.toc completed successfully.

我希望distributable能够以与原始代码相同的方式工作,而不会出现分段错误。对于如何直接解决此问题或追踪故障本身的任何帮助/提示,我们将不胜感激。你知道吗


Tags: offrompyimportselfinfobackendsqlalchemy