cx_Freeze converted GUI-app (tkinter) crashes after pressing plot button - python
I've been dealing with this for days now and hope to find some help. I developed a GUI-application with imported modules tkinter, numpy, scipy, matplotlib, which runs fine in python itself. After having converted to an exe everything works as expected, but NOT the matplotlib section. When I press my defined plot button, the exe simply closes and doesn't show any plots.
So I thought to make a minimal example, where I simply plot a sin-function and I'm facing the same issue:
Works perfect in python, when converting it to an exe it crashes when pressing the plot button. Here is the minimal example:
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
class MainWindow(tk.Frame):
def __init__(self):
tk.Frame.__init__(self,bg='#9C9C9C',relief="flat", bd=10)
self.place(width=x,height=y)
self.create_widgets()
def function(self):
datax = np.arange(-50,50,0.1)
datay = np.sin(datax)
plt.plot(datax,datay)
plt.show()
def create_widgets(self):
plot = tk.Button(self, text='PLOT', command=self.function)
plot.pack()
x,y=120,300
root=tk.Tk()
root.geometry(str(x)+"x"+str(y))
app = MainWindow()
app.mainloop()
And here is my corresponding setup.py for converting with cx_Freeze:
import cx_Freeze
import matplotlib
import sys
import numpy
import tkinter
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [cx_Freeze.Executable("test.py", base=base)]
build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot",
"tkinter.filedialog","numpy"],
"include_files":[(matplotlib.get_data_path(), "mpl-data")],
"excludes":[],
}
cx_Freeze.setup(
name = "test it",
options = {"build_exe": build_exe_options},
version = "1.0",
description = "I test it",
executables = executables)
Any ideas that might solve the issue are highly appreciated. I'm working on a 64-bit Windows10 machine and I'm using the WinPython Distribution with Python 3.4.3.
I found a potential solution (or at least an explanation) for this problem while testing PyInstaller with the same test.py. I received error message about a dll file being missing, that file being mkl_intel_thread.dll.
I searched for that file and it was found inside numpy folder.
I copied files matching mkl_*.dll and also libiomp5md.dll to the same directory where the test.exe created by python setup.py build was. After this the minimal test.exe showed the matplotlib window when pressing the plot button.
The files were located in folder lib\site-packages\numpy\core.
I really wanted to post this as a comment, but I don't have the reputation. This is mostly a followup to J.J. Hakala's answer about how to find the cause.
If one changes the base to "Console", i.e. using
base = "Console"
rather than
base = "Win32GUI"
a console will also pop up when the program starts and this error is printed
Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.
Which can help finding the cause of the problem pretty faster.
I thought this would be worth mentioning, since this trick can also be helpful to diagnose other problems. In the final release, one can revert back to Win32GUI to avoid the extra console. I should give the credits to this other stackoverflow post
I have followed #J.J. Hakala's answer, but I found that it's not necessary copy all mkl_*.dll and libiomp5md.dll files. For me it worked with libiomp5md.dll mkl_core.dll mkl_def.dll mkl_intel_thread.dll. This helps to reduce the final bundle size in ~500MB.
Also, you can include the files you want to copy in the include_files option. You also could only want to include them if sys.platform is win32.
I'm using Anaconda as well as #Matt Williams, so, changing a bit the OP's code:
import cx_Freeze
import matplotlib
import sys
import numpy
import tkinter
import os
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot",
"tkinter.filedialog","numpy"],
"include_files":[(matplotlib.get_data_path(), "mpl-data")],
"excludes":[],
}
base = None
if sys.platform == "win32":
base = "Win32GUI"
DLLS_FOLDER = os.path.join(PYTHON_INSTALL_DIR, 'Library', 'bin')
dependencies = ['libiomp5md.dll', 'mkl_core.dll', 'mkl_def.dll', 'mkl_intel_thread.dll']
for dependency in dependencies:
build_exe_options['include_files'].append(os.path.join(DLLS_FOLDER, dependency))
executables = [cx_Freeze.Executable("test.py", base=base)]
cx_Freeze.setup(
name = "test it",
options = {"build_exe": build_exe_options},
version = "1.0",
description = "I test it",
executables = executables)
Check if you have numpy+mkl package installed. Uninstalling numpy and installing numpy+mkl package solved my issue of getting error related to mkl_intel_thread.dll
Related
Mac cxFreeze app quits unexpectedly when trying to open
I am trying to convert a python script to a mac app so I can distribute it. I'm using cxFreeze to do this. After creating the app, I try to open it but it says the app quit unexpectedly and shows some report. (code signature invalid (errno=1) usr/local/lib/Python (no such file) --- my script at.py: import tkinter as tk from tkinter import font window = tk.Tk() width=1 window.title('test') window.geometry("425x500") label_speed = tk.Label( text="Speed" ) label_speed.grid(row=1, column=1, columnspan = 5, stick="w") window.mainloop() And then my setup.py from cx_Freeze import Executable, setup base = None if sys.platform == "win32": base = "Win32GUI" executables = [Executable("at.py", base=base)] setup( name="test", version="0.1", description="just for testing", executables=executables, ) I used the following commands to make the mac bundle or app. python3 setup.py build then python3 setup.py bdist_dmg I had to use python3 instead of python because it wasn't working for me. Thanks in advance for any tips and answers
There might be two different things going on. The first thing I know I have run into with cx_freeze is that it tried to map to where it thinks the python 2.x folder should be even if I specify to run on python 3.x. The other thing might be it was downloaded on to a different path. type $ where python to see where the file path should be. If you do $ open $FILEPATH and you see that its using python3 it might be worth reaching out to the maintainer of cx_freeze and see if they have any advice.
cx_Freeze exe file opens and then closes
My code converter code is down here: import os os.environ['TCL_LIBRARY'] = "C:\\Program Files (x86)\\Python35-32\\tcl\\tcl8.6" os.environ['TK_LIBRARY'] = "C:\\Program Files (x86)\\Python35-32\\tcl\\tk8.6" from cx_Freeze import setup, Executable if __name__.endswith('__main__'): setup(name = "Dodge The Blocks", version = "2.0", description = 'A fun little game for when your bored', executables = [Executable(r"C:\Users\Harshal\Desktop\GameWIP.py")] ) I also wanted to ask that what does this mean: if __name__.endswith('__main__'):
Regarding the first part of your question: see this answer for a working setup script to freeze an application relying on tkinter using cx_Freeze 5.1.1 (and also see my comment to your question) Regarding the second part of your question what does this mean: if __name__.endswith('__main__'): See What does if __name__ == "__main__": do? You probably got the modified version you are using from this issue or a similar one. In any case, this line and its modification might be relevant for the main application, but is not necessary in the setup script and should be removed from there.
GUI application runs in Spyder and cmd but not as a standalone .exe [duplicate]
I've been dealing with this for days now and hope to find some help. I developed a GUI-application with imported modules tkinter, numpy, scipy, matplotlib, which runs fine in python itself. After having converted to an exe everything works as expected, but NOT the matplotlib section. When I press my defined plot button, the exe simply closes and doesn't show any plots. So I thought to make a minimal example, where I simply plot a sin-function and I'm facing the same issue: Works perfect in python, when converting it to an exe it crashes when pressing the plot button. Here is the minimal example: import tkinter as tk import matplotlib.pyplot as plt import numpy as np class MainWindow(tk.Frame): def __init__(self): tk.Frame.__init__(self,bg='#9C9C9C',relief="flat", bd=10) self.place(width=x,height=y) self.create_widgets() def function(self): datax = np.arange(-50,50,0.1) datay = np.sin(datax) plt.plot(datax,datay) plt.show() def create_widgets(self): plot = tk.Button(self, text='PLOT', command=self.function) plot.pack() x,y=120,300 root=tk.Tk() root.geometry(str(x)+"x"+str(y)) app = MainWindow() app.mainloop() And here is my corresponding setup.py for converting with cx_Freeze: import cx_Freeze import matplotlib import sys import numpy import tkinter base = None if sys.platform == "win32": base = "Win32GUI" executables = [cx_Freeze.Executable("test.py", base=base)] build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot", "tkinter.filedialog","numpy"], "include_files":[(matplotlib.get_data_path(), "mpl-data")], "excludes":[], } cx_Freeze.setup( name = "test it", options = {"build_exe": build_exe_options}, version = "1.0", description = "I test it", executables = executables) Any ideas that might solve the issue are highly appreciated. I'm working on a 64-bit Windows10 machine and I'm using the WinPython Distribution with Python 3.4.3.
I found a potential solution (or at least an explanation) for this problem while testing PyInstaller with the same test.py. I received error message about a dll file being missing, that file being mkl_intel_thread.dll. I searched for that file and it was found inside numpy folder. I copied files matching mkl_*.dll and also libiomp5md.dll to the same directory where the test.exe created by python setup.py build was. After this the minimal test.exe showed the matplotlib window when pressing the plot button. The files were located in folder lib\site-packages\numpy\core.
I really wanted to post this as a comment, but I don't have the reputation. This is mostly a followup to J.J. Hakala's answer about how to find the cause. If one changes the base to "Console", i.e. using base = "Console" rather than base = "Win32GUI" a console will also pop up when the program starts and this error is printed Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll. Which can help finding the cause of the problem pretty faster. I thought this would be worth mentioning, since this trick can also be helpful to diagnose other problems. In the final release, one can revert back to Win32GUI to avoid the extra console. I should give the credits to this other stackoverflow post
I have followed #J.J. Hakala's answer, but I found that it's not necessary copy all mkl_*.dll and libiomp5md.dll files. For me it worked with libiomp5md.dll mkl_core.dll mkl_def.dll mkl_intel_thread.dll. This helps to reduce the final bundle size in ~500MB. Also, you can include the files you want to copy in the include_files option. You also could only want to include them if sys.platform is win32. I'm using Anaconda as well as #Matt Williams, so, changing a bit the OP's code: import cx_Freeze import matplotlib import sys import numpy import tkinter import os PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__)) build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot", "tkinter.filedialog","numpy"], "include_files":[(matplotlib.get_data_path(), "mpl-data")], "excludes":[], } base = None if sys.platform == "win32": base = "Win32GUI" DLLS_FOLDER = os.path.join(PYTHON_INSTALL_DIR, 'Library', 'bin') dependencies = ['libiomp5md.dll', 'mkl_core.dll', 'mkl_def.dll', 'mkl_intel_thread.dll'] for dependency in dependencies: build_exe_options['include_files'].append(os.path.join(DLLS_FOLDER, dependency)) executables = [cx_Freeze.Executable("test.py", base=base)] cx_Freeze.setup( name = "test it", options = {"build_exe": build_exe_options}, version = "1.0", description = "I test it", executables = executables)
Check if you have numpy+mkl package installed. Uninstalling numpy and installing numpy+mkl package solved my issue of getting error related to mkl_intel_thread.dll
Python crashes when trying to compile using cx_Freeze
I'm trying to compile my python scripts using cx_Freeze, here is my setup file: import cx_Freeze import sys import matplotlib import os base = None if sys.platform == 'win32': base = "Win32GUI" os.environ['TCL_LIBRARY'] = r'C:\\Python35\\tcl\\tcl8.6' os.environ['TK_LIBRARY'] = r'C:\\Python35\\tcl\\tk8.6' executables = [cx_Freeze.Executable("HomeScreen.py", base=base, icon="icon.png")] cx_Freeze.setup( name = "LeagueBoost", options = {"build_exe":{"packages": ["sqlite3","requests","time","sys","os","statistics","matplotlib","random","collections"], "include_files": ["Assets", "LeagueBoost_v1.py","LBRun.py","graphSetup.py","profilepage.py","Assets_rc.py"]}}, version = "1", executables = executables ) But when I give the cmd command C:/python35/python.exe, it gets to copying C:\python35\python35.dll -> build\exe.win-amd64-3.5\python35.dll it pops up "python has stopped working"
This is crazy after hitting my head against the wall for the weird reason python crashes when I tried to build executable with cx_Freeze, what solved my problem is using ico format for icon file. Your icon file should be icon type not png, may be because png is not supported by cx_Freeze. In your setup.py change icon="icon.png" to icon="icon.ico", please note the icon file must be in ico format, don't act smart and just change the extension. If it still doesn't work you can give it a trial without writing this option at all icon="icon.png" and see if it works.
is cx-freeze incompatible with python for .net?
for sake of testing I am using demo from https://github.com/pythonnet/pythonnet/blob/master/demo/wordpad.py to convert it to .exe using cx_freeze==5.0. but it shows missing clr modules (obviosly). How to work around with this? import sys from cx_Freeze import setup, Executable setup( name = "WordPad", version = "3.1", description = "A word pad demo", executables = [Executable("main.pyw", base = "Win32GUI")]) Disclaimer: This is my first attempt using cx_freeze.
This auto-discovery of pythonnet was merged to cx_freeze: https://github.com/anthony-tuininga/cx_Freeze/pull/251