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。
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')]
希望有帮助。