【发布时间】:2020-12-07 17:57:26
【问题描述】:
我无法从我的 Python 脚本创建一个有效的 exe 文件。我能够将其范围缩小到 matplotlib 的使用范围,但我不知道如何解决。
我创建 exe 文件,使用pyinstaller --onefile myScript.py 命令。 exe 是使用一些“忽略”的导入创建的,例如:
信息:Matplotlib 后端“GTK3Agg”:忽略 后端 Gtk3Agg 需要 cairo
当我运行 exe 时,会出现一个命令窗口,给出一个错误,然后自行终止:
错误:运行时错误找不到 matplotlib 数据文件
如果我从 Python 终端运行代码,一切正常。 如果我删除 matplotlib 和图形相关的线,exe 会正确创建。
一些 (pyinstaller and matplotlib - error while executring .exe) 声称解决方案是更新 pyinstaller。我做了,它没有帮助。
有人建议不要使用plt (Python Matplotlib runtime error upon closing the console),但后来我无法使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