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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
0

I had the same issue. Had to load and build GTK library to satisfy all the missing libs for the build. The executable works, but the final app is too slow.

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.