1

I am failing to create a working exe file from my Python script. I was able to narrow it down to matplotlib usage, but I do not know how to fix.

I create exe file, using pyinstaller --onefile myScript.py command. exe is created with some "ignored" imports such as:

INFO: Matplotlib backend "GTK3Agg": ignored backend Gtk3Agg requires cairo

When I run exe, a command window appears, gives an error and then terminates itself:

ERROR: Runtime error could not find the matplotlib data files

If I run the code from Python terminal, everything works fine. If I remove matplotlib and graph related lines, exe is created properly.

Some (pyinstaller and matplotlib - error while executring .exe) claims the solution is to update pyinstaller. I did, it did not help.

Some suggests not using plt (Python Matplotlib runtime error upon closing the console), but then I couldn't make subplot function work.

I feel this link (http://www.py2exe.org/index.cgi/MatPlotLib) is related but I do not understand what exactly I need to do. I got other errors when I try.

Here is my code:

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 

Note: I am running Python 3.8.3, on Windows 10, pip version 20.2.2.

neder
  • 13
  • 3

1 Answers1

1

If you use a .spec file, I had to add this in mine to get rid of that error:

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')]

Hopefully that helps.

Ross Wardrup
  • 311
  • 1
  • 9
  • 26
  • Thank you for helping, Ross. I do not use a spec file, but I noticed there is one in the working directory. Thus I pasted your lines into that file, above everything and then tried again. Exe creation is without warnings now, but when I double click exe file, it still complains that matplotlib file cannot be found. Exe is still not working. Do I also need to copy lib file to same folder? – neder Aug 24 '20 at 07:12