exe made by pyinstaller crashes at calling matplotlib - python

I am trying to use matplotlib with Tkinter to create a stand-alone executable by pyinstall (command: pyinstaller --onefile main.py).
The program works at the stations where the python installed. However, at the computers without any installed python the program crashes at the line, which calls pyplot (fig = plt.Figure()). The crash occurs without any error.
I've tried upgrading/downgrading matplotlib or pyinstaller, changed Figure() to figure(), re-installed numpy, nothing helped, and I do not know what to do else. I ran it from command prompt and I haven't seen any messages.
UPD: I've tried --debug-imports flag, and found the only line that differs between working and not working station is "exec(bytecode, module.dict)" that exists only in the debug-log of the working program. The line occurs after deprecation warning "D:\Prog_files\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:493: MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3."
Do you have any ideas on how I can fix it?
Code:
from tkinter import *
from tkinter import filedialog
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def setplot(x, y):
fig = plt.Figure()
canvas = FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
ax1 = fig.add_subplot(1, 1, 1)
(l, ) = ax1.plot(x, y)
def Quit():
global root
root.destroy()
def LoadFile():
xx = list(range(0,100))
yy = [i*i for i in xx]
setplot(xx, yy)
root = Tk()
root.geometry('700x500')
panelFrame = Frame(root, height = 60, bg = 'gray')
panelFrame.pack(side = 'top', fill = 'x')
loadBtn = Button(panelFrame, text = 'Plot', command = LoadFile)
quitBtn = Button(panelFrame, text = 'Exit', command = Quit)
loadBtn.pack()
loadBtn.place(x = 10, y = 10, width = 70, height = 40)
quitBtn.place(x = 100, y = 10, width = 70, height = 40)
root.mainloop()

I had a similar problem and after several searches I found a recipe that works. These are my configurations with Anaconda:
python version 3.6
pyinstaller version 3.6
matplotlib version 3.0.3
Do not use pyinstaller with "--onefile" option so in the folder "dist" there will be various ".dll" files. And here is the problem, the file "libiomp5md.dll" is missing!
Then just copy to the folder "dist" the file "libiomp5md.dll" which is located in the Anaconda installation folder ...\Anaconda3\Library\bin

I had the same exact issue in a project that used
matplotlib
pandas
pillow
pyinstaller
pysimplegui
After endless hours trying a million different solutions (DLLs, cmd line parameters, --paths="", hooks, reading the docs, trying cx freeze and py2exe instead etc.) i fixed it randomly by simply using a specific combination of package versions... so frustrating...
python 3.7.10
matplotlib 3.4.1
pyinstaller 4.3
pysimplegui 4.38
The rest of the packages that I mentioned don't seem to have made a difference. I really hope it helps, i know how painful it is. I suggest creating a new anaconda environment from scratch and installing the specific versions with conda install -c conda-forge -n {envNameYouWant} pyinstaller=4.3

Related

Python Playsound not playing any sound when compiled into an .exe file

I'm trying to create a digital soundboard that can play object-defined sounds using tkinter and the playsound module. When I'm testing it via VS code, it works without any problems. But after I compile it into an exe, the app works but there's no sound playing.
I tried changing my compilation mode from '--onefile' to '--onedir' in pyinstaller but it didn't work. I also place the sound files into the folder directory of the executable file, but to no avail.
Could there be something wrong with my code?
my code:
import tkinter
from tkinter import *
from PIL import ImageTk, Image
from playsound import playsound
win = Tk()
win.geometry('500x500')
win.maxsize(500,500)
win.title('Alarm Button')
def alarm():
playsound(r'Announcement.mp3')
btn_image = PhotoImage(file='BUTTON.png')
comp_logo = PhotoImage(file='aaaa.png')
Logo = Label(win, image = comp_logo).place(x=390, y=470)
Press_me = Button(win, image=btn_image, command=alarm, borderwidth = 0)
Press_me.place(x=45, y=15)
def handler(e):
alarm()
win.bind('<Return>', handler)
win.mainloop()
I tried it and it still works, maybe it's because of pyinstaller? Or you can try using auto-py-to-exe since it works for me and also easier to use: https://pypi.org/project/auto-py-to-exe/
Make sure that you use playsound 1.2.2 because it's more stable than the 1.3.0 version. You can do it by using:
pip uninstall playsound
Then:
pip install playsound==1.2.2
Also I see that you import tkinter 2 times? You should delete
import tkinter

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.

Python Tkinter DnD2 root = TkinterDnD.Tk() Unable to load tkdnd library

I installed TkDnD as instructed here: How to Install and Use TkDnD with Python 2.7 Tkinter on OSX?
Mainly i followed this advice:
On Windows:
1) Copy the tkdnd2.8 directory to C:\Python27\tcl
2) Copy the TkinterDnD2 directory to C:\Python27\Lib\site-packages
I am using anaconda so I copied it into my environments directories (C:\ProgramData\Anaconda3\envs\gui)
And yet when i try to run this code:
import sys
if sys.version_info[0] == 2:
from Tkinter import *
else:
from tkinter import *
from TkinterDnD2 import *
def drop(event):
entry_sv.set(event.data)
root = TkinterDnD.Tk()
entry_sv = StringVar()
entry_sv.set('Drop Here...')
entry = Entry(root, textvar=entry_sv, width=80)
entry.pack(fill=X, padx=10, pady=10)
entry.drop_target_register(DND_FILES)
entry.dnd_bind('<<Drop>>', drop)
root.mainloop()
I get this error
Exception has occurred: RuntimeError
Unable to load tkdnd library.
File "drag_and_drop_GUI.py", line 10, in <module>
root = TkinterDnD.Tk()
Thinking it's a problem with anaconda I installed TkDnD in my system Python too (no virtual environments) but the issue still persists.
Any ideas ?
Try installing the tkdnd binary matching your python installation (64 bit or 32 bit).

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

cx_Freeze converted GUI-app (tkinter) crashes after pressing plot button

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

Categories