Pyinstaller:QWebEngineView 不能很好地与其他模块一起使用

Pyinstaller: QWebEngineView doesn't play nicely with other modules

我有一个同时使用 QWebEngineView 和 Matplotlib 的应用程序。具体来说,只要导入Matplotlib就足以破坏生成的executable中的QWebEngineView。这是 works/doesn 在我的 Windows 10 机器上无法工作的 table。我无法在 Linux 上重现此内容。遗憾的是部署的EXE文件必须在Windows.

编辑:不仅仅是Matplotlib。 QWebEngineView 也与 pyqtgraph and numpy.

行为不当
QWebEngineView Matplotlib test.html renders OK?
run from python X YES
run from python X X YES
run from EXE X YES
run from EXE X X NO

这是 test.html 的“正常”渲染。它有一个格式化的标题、一个 table 和一个 SVG 图像:

这是同一文件的错误呈现:

Matplotlib 中的某些东西搞砸了 QWebEngine 渲染。是否有针对此行为的某种解决方法?

代码:

from PyQt5 import QtWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView

# ~ import matplotlib.pyplot as plt  # <------ this is the line that breaks it


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    
    view = QtWidgets.QWidget()
    layout = QtWidgets.QVBoxLayout(view)
    
    lineedit = QtWidgets.QLineEdit()
    webview =  QWebEngineView()
    
    layout.addWidget(lineedit)
    layout.addWidget(webview)
    
    lineedit.setText('file:///test.html')
    
    def handleReturn():
        qurl = QUrl(lineedit.text())
        webview.setUrl(qurl)
    
    lineedit.returnPressed.connect(handleReturn)
    handleReturn()
    
    view.show()

    sys.exit(app.exec_())

Pyinstaller 规范文件:

# -*- mode: python -*-
# this file gives build options for pyinstaller

added_files = [ ('qt.conf',   '.'),
                ('test.html', '.'),
                ('test.svg',  '.') ]

a = Analysis(['webengine.py'],
             datas= added_files)
pyz = PYZ(a.pure, a.zipped_data,)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='webengine',
          debug=True,
          strip=False,
          upx=False,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               name='webengine')

test.html

<html><body>
<p><h1>Test HTML Document</h1></p>
<table style="width:80%" border="1">
<tr><th>C1</th><th>C2</th><th>C3</th><th>C4</th></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>2</td><td>4</td><td>6</td><td>8</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td></tr>
<tr><td>the</td><td>quick</td><td>brown</td><td>fox</td></tr>
<tr><td>jumped</td><td>over</td><td>the lazy</td><td>dog</td></tr>
</table>
<p><img src="test.svg" width="50%"></p>
</body></html>

test.svg:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="svg2" width="620" height="472" xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs id="defs4">
  <path id="box1" d="m0 0h77v210h-77z" stroke="#000" stroke-width="2"/>
  <path id="box2" d="m0 0h77v60h-77z" stroke="#000" stroke-width="2"/>
 </defs>
 <path id="bg" d="m0 0h620v472h-620z" fill="#fff"/>
 <g id="g9" transform="translate(2 1)">
  <use id="use11" fill="#fff" xlink:href="#box1"/>
  <use id="use13" x="77" fill="#ff0" xlink:href="#box1"/>
  <use id="use15" x="154" fill="#0ff" xlink:href="#box1"/>
  <use id="use17" x="231" fill="#0f0" xlink:href="#box1"/>
  <use id="use19" x="308" fill="#f0f" xlink:href="#box1"/>
  <use id="use21" x="385" fill="red" xlink:href="#box1"/>
  <use id="use23" x="462" fill="#00f" xlink:href="#box1"/>
  <use id="use25" x="539" xlink:href="#box1"/>
 </g>
 <g id="g45" transform="translate(2 220)">
  <use id="use47" fill="#0f0" xlink:href="#box2"/>
  <use id="use49" x="77" fill="#0f0" xlink:href="#box2"/>
  <use id="use51" x="154" fill="#0f0" xlink:href="#box2"/>
  <use id="use53" x="231" fill="#0f0" xlink:href="#box2"/>
  <use id="use55" x="308" fill="#fff" xlink:href="#box2"/>
  <use id="use57" x="385" fill="#fff" xlink:href="#box2"/>
  <use id="use59" x="462" fill="#fff" xlink:href="#box2"/>
  <use id="use61" x="539" fill="#fff" xlink:href="#box2"/>
  <text id="green100" x="30" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">0.59</text>
 </g>
 <g id="g27" transform="translate(2 280)">
  <use id="use29" fill="red" xlink:href="#box2"/>
  <use id="use31" x="77" fill="red" xlink:href="#box2"/>
  <use id="use33" x="154" fill="#fff" xlink:href="#box2"/>
  <use id="use35" x="231" fill="#fff" xlink:href="#box2"/>
  <use id="use37" x="308" fill="red" xlink:href="#box2"/>
  <use id="use39" x="385" fill="red" xlink:href="#box2"/>
  <use id="use41" x="462" fill="#fff" xlink:href="#box2"/>
  <use id="use43" x="539" fill="#fff" xlink:href="#box2"/>
  <text id="red100" x="20" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">+0.30</text>
 </g>
 <g id="g63" transform="translate(2 340)">
  <use id="use65" fill="#00f" xlink:href="#box2"/>
  <use id="use67" x="77" fill="#fff" xlink:href="#box2"/>
  <use id="use69" x="154" fill="#00f" xlink:href="#box2"/>
  <use id="use71" x="231" fill="#fff" xlink:href="#box2"/>
  <use id="use73" x="308" fill="#00f" xlink:href="#box2"/>
  <use id="use75" x="385" fill="#fff" xlink:href="#box2"/>
  <use id="use77" x="462" fill="#00f" xlink:href="#box2"/>
  <use id="use79" x="539" fill="#fff" xlink:href="#box2"/>
  <text id="blue100" x="20" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">+0.11</text>
 </g>
 <g id="g63" transform="translate(2 410)">
  <use id="grey100" fill="#fff" xlink:href="#box2"/>
  <use id="grey89" x="77" fill="#e3e3e3" xlink:href="#box2"/>
  <use id="grey70" x="154" fill="#b2b2b2" xlink:href="#box2"/>
  <use id="grey59" x="231" fill="#969696" xlink:href="#box2"/>
  <use id="grey41" x="308" fill="#696969" xlink:href="#box2"/>
  <use id="grey30" x="385" fill="#4d4d4d" xlink:href="#box2"/>
  <use id="grey11" x="462" fill="#1c1c1c" xlink:href="#box2"/>
  <use id="grey0" x="539" fill="#000" xlink:href="#box2"/>
  <text id="txgrey100" x="20" y="35" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">100%</text>
  <text id="txgrey89" x="102" y="35" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">89%</text>
  <text id="txgrey70" x="179" y="35" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">70%</text>
  <text id="txgrey59" x="256" y="35" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">59%</text>
  <text id="txgrey41" x="333" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">41%</text>
  <text id="txgrey30" x="408" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">30%</text>
  <text id="txgrey11" x="487" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">11%</text>
  <text id="txgrey0" x="569" y="35" fill="#fff" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="1" xml:space="preserve">0%</text>
 </g>
 <text id="text3446-0" x="90" y="184" fill="#fff" font-size="180" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="4" xml:space="preserve">TEST</text>
 <text id="text3446" x="80" y="174" font-family="DejaVu Sans, Arial, Helvetica" stroke-width="4" xml:space="preserve"><tspan id="tspan3448" x="80" y="174" font-size="180">TEST</tspan></text>
</svg>

我在另一台 Windows 机器上“编译”并且 QWebEngineView 呈现得很好。所以我不得不弄清楚“坏”机器和“好”机器之间的区别。

要列出已安装的 python 模块及其版本,您可以使用

pip list

我不知道这是否会收集您在 pip 之外安装的模块(例如,在 Windows 中通过 .msi 安装或在 Debian Linux

中通过 apt install python3-<python module> 安装

我不太确定哪个包有问题。我没有耐心一一去——太费时间了。但这是一个有效的列表:

Package Version
altgraph 0.17
cycler 0.10.0
future 0.18.2
importlib-metadata 3.4.0
kiwisolver 1.3.1
matplotlib 3.4.2
numpy 1.21.1
pandas 1.2.1
pdfkit 0.6.1
pefile 2021.5.24
Pillow 8.1.2
pip 21.0.1
pycparser 2.20
pyinstaller 4.5.1
pyinstaller-hooks-contrib 2021.2
pyparsing 2.4.7
PyQt5 5.15.4
PyQt5-Qt5 5.15.2
PyQt5-sip 12.8.1
pyqtgraph 0.11.1
PyQtWebEngine 5.15.4
PyQtWebEngine-Qt5 5.15.2
pyserial 3.5
python-dateutil 2.8.1
pythonnet 2.5.2
pytz 2021.1
pyusb 1.1.1
PyVISA 1.11.3
pywin32 301
pywin32-ctypes 0.2.0
scipy 1.6.1
setuptools 47.1.0
six 1.15.0
typing-extensions 3.7.4.3
zipp 3.4.0