首页 > 解决方案 > Pyinstaller 和 Matplotlib 的问题。文件 .exe 不起作用并且没有给出任何错误

问题描述

一般来说,我对 Python 和程序语言都很陌生。

我已经一个多星期没有在这方面取得任何进展,所以最后我试着在这里问(我的第一次!)。

和许多其他人一样(我到处搜索,但找不到任何可以帮助我的东西),我试图成功获得一个exe。使用 Pyinstaller从 Python 脚本(内部带有matplotlib模块)生成文件。

双击exe文件时当然会出现问题(使用anaconda或python编译器我没有任何问题)

我知道问题与matplolib及其相关行有关(如果我评论它们,exe 文件运行良好)

我尝试了很多东西,比如降级 matplotlib、pyinstaller、使用不同的计算机和版本的 python、使用不同的类型来绘制,但这些都不起作用。

以下是一个不起作用的脚本的简单示例 `

import matplotlib
from matplotlib.figure import Figure
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig= Figure()

ax = fig.add_subplot(111)
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")`enter code here`

我检测到问题从前一个案例“ fig= Figure() ”中的第一个 matplotlib 命令开始,但其他时候是“ fig, ax = plt.subplots() ”或“plt.show() ”(当然有将 matplotli.pyplot 导入为 plt)"

另一个问题是,当在一段时间后双击 .exe 时,窗口关闭而没有任何明确的错误消息

使用--debug=all崩溃前的最后一行运行 pyinstaller如下在此处输入图像描述

所以它停在import matplotlib.pyplot(在附加的脚本中,因为它停在import matplotlib.figure)。我还通过--hidden-import=matplotlib.pyplot将这些导入添加到隐藏导入,但没有任何改变。

有人有一些建议吗?请帮我解决这个问题:)

pyinstaller 版本 4.2 python 3.8.5 / 3.7 / 3.9.4 matplotlib 3.1.3(从 3.4.1 降级)

赢10

非常感谢您的建议,

斯特凡诺

编辑

极好的!最后,我通过安装没有任何 --add.data, hidden file ext 的 DEV 版本的 pyinstaller 成功解决了这个问题:

pip install github.com/pyinstaller/pyinstaller/archive/develop.zip

标签: pythonmatplotlibcrashpyinstaller

解决方案


推荐阅读