Matplotlib 在独立(Exe)Python 文件中的用法

问题描述 投票:0回答:2
我无法从我的 Python 脚本创建工作

exe 文件。我能够将其范围缩小到 matplotlib 的使用,但我不知道如何修复。

我使用

pyinstaller --onefile myScript.py 命令创建 exe

 文件。 
exe 是通过一些“忽略”的导入创建的,例如:

信息:Matplotlib 后端“GTK3Agg”:被忽略 后端 Gtk3Agg 需要 cairo

当我运行

exe 时,会出现一个命令窗口,给出错误,然后自行终止:

错误:运行时错误找不到 matplotlib 数据文件

如果我从 Python 终端运行代码,一切都会正常。 如果我删除 matplotlib 和图形相关线,则

exe 会正确创建。

一些(

pyinstaller 和 matplotlib - 执行 .exe 时出错)声称解决方案是更新 pyinstaller。我做了,但没有帮助。

有人建议不要使用

plt

关闭控制台时出现Python Matplotlib运行时错误),但后来我无法使subplot
函数工作。

我觉得这个链接(

http://www.py2exe.org/index.cgi/MatPlotLib)是相关的,但我不明白我到底需要做什么。当我尝试时,我遇到了其他错误。

这是我的代码:

import tkinter as tk import tkinter.ttk as ttk import matplotlib.pyplot as plt class myApp(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.parent = parent myFrame = ttk.LabelFrame(self.parent, text="FRAME", borderwidth=5) myFrame.grid(row=0, column=2, padx=5, pady=5, sticky='NS') myButton = ttk.Button(myFrame, width=12, text="SHOW GRAPH", command=self.showGraph) .grid(row=0, column=1, sticky=tk.S) def showGraph(self): fig, axs = plt.subplots(2, 3) plt.show(block=False) def main(root): app = myApp(root) root.wm_title("MyApp") root.mainloop() if __name__ == "__main__": root = tk.Tk() main(root) else: pass
注意:我在 Windows 10 上运行 Python 3.8.3,pip 版本 20.2.2。

python matplotlib pyinstaller python-standalone
2个回答
1
投票
如果您使用 .spec 文件,我必须在我的文件中添加此文件以消除该错误:

from PyInstaller.utils.hooks import exec_statement mpl_data_dir = exec_statement("import matplotlib; print(matplotlib._get_data_path())") datas=[(mpl_data_dir, 'matplotlib\\mpl-data')]
希望有帮助。


0
投票
我也有同样的问题。必须加载并构建 GTK 库以满足构建所需的所有缺少的库。可执行文件可以工作,但最终的应用程序太慢了。

© www.soinside.com 2019 - 2024. All rights reserved.