Pyinstaller onefile Executable Incredibly Huge and Slow - python

I know I'm not the first to ask, but the other answers on the forum could not help me, so I'm asking. I have a short (181 line) python script that only has the imports
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, RadioButtons
and runs up a interactive math tool with sliders and radio buttons. But when I built it with pyinstaller, I end up with a massive (90 MB) dist folder that appears to include all sorts of unnecessary packages like babel and cryptography. The executable inside takes around 5 seconds or so to actually display the figure. When I run it with the --onefile option, things seem no better, as I get a 43 MB executable that takes even longer, about 10 seconds, to display the figure.
I'm prety sure it's not the script's fault because in Spyder, it takes less than a second to open up. I'm suspecting that it is because of all the extraneous packages. If so, how do I get pyinstaller to exclude them, and if not, then what is the likely issue? Thanks.
EDIT: Also, I am doing all of this in a conda virtual environment, which I read somewhere should already help to make pyinstaller's executable smaller. After creating the environment, the only packages I directly installed were pyinstaller, spyder, numpy, and matplotlib.

This happens because when creating the executable you are also using the libraries, what I do is import only what is necessary for the program and then compile it, for example, for an application in Tkinter:
from tkinter import Tk, Label
root = Tk()
Label(root, text='Label').pack()
root.mainloop()

Related

Pyinstaller onefile doesn't work on other computers

My role at work includes automating several processes which are easily automated via Python, for use by the entire team at the office. None of my coworkers have Python set up on their devices (we're all Windows) nor would they be comfortable using it if they did, so I've been compiling my work into .exe files with tkinter.
My most recent (and, of course, the most important) program compiles into a onefile .exe without issue, and runs perfectly on my computer. My coworkers are able to open it and load files into it, but when they go to activate some of the tkinter buttons within, one coworker has absolutely nothing happen while another gets a windows error message that the program has crashed. I'm unable to recreate these issues on my side.
The .exe was made using pyinstaller in a virtual environment, in which I installed all necessary dependencies. The script does not need to read or reference any other files in order to run. Is it possible that the .exe is still somehow missing a dependency that it's finding on my device and not others? How can I be sure? Would setting it up to include an installer wizard make a difference? Thanks for the help on this!
I can't include the full script due to character limit, but I've included the opening lines with all imports.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.fft import fft as fft
from scipy.fft import ifft as ifft
from tkinter import *
from tkinter import ttk
from tkinter import simpledialog
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

creating standalone exe using pyinstaller with mayavi import

I have a program that helps visualize some data in 3D by plotting a surface and a cloud of points to see how they relate to the surface. For the visualization I am using mayavi since it was easy to set up and does a better job than matplotlib in 3D. The program works great when I run it in my python environment and makes beautiful visualizations. However, I need to distribute this to a few people who don't have Python and would prefer not to have to install python and all the add-ins on each computer, so I have been using pyinstaller to create standalone .exe files that they can run after I develop a program.
For reference, this is all done on Windows 10, Python 3.6, pyqt 4.11.4, pyface 6.0.0, traits 4.6.0, pyinstaller 3.3.1, mayavi 4.5.0+vtk81. Just about every module I use was installed using pip.
The problem is that I can't seem to get a working exe if I use/import the mayavi module. I have been reading so much github documentation on different hook files and hidden-imports and each time I fix one error another pops up. It all started with scipy but I believe I have those worked out. So I have a few questions that could help me solve the problem:
1) Has anyone successfully created a standalone exe using pyinstaller with a mayavi import (specifically from mayavi import mlab)? What is your secret?!?
This seems similar but I haven't been able to figure it out yet... SO_link
I have used the following links (scipy,h5py,pandas,traits/qt4,ETS_TOOLKIT) to add hidden imports or fix other issues but I am stuck now after setting my ETS_TOOLKIT=qt4. Before setting it, I would get a pyface/traits error RuntimeError: No traitsui.toolkits plugin found for toolkit null, but now it says the same thing for qt4 instead of null. I have qt4 installed so I don't understand that... It is in the import_toolkit function in pyface/base_toolkit.py.
2) Is there a better route to go in terms of 3D visualization / exe creation? I need a 3D program that can accurately render if the points are in front of or behind the surface and be able to rotate/zoom/pan interactively, plus it needs to be intuitive. Mayavi had very simple commands similar to matplotlib but others seem very complicated working around how the UI interacts with everything.
3) How do I interpret all of these error codes I get? They are usually pretty cryptic saying a certain line threw an exception nested in 10 other function calls and it seems very difficult to back out where exactly things went wrong, especially when nothing pops up on Google that seems to be related.
EDIT
While I am still very confused, I have been able to change where the error occurs. Following the traceback, I commented out a line setting the marker color in traitsui/editors/code_editor.py (line 49), at which point the exception then started the next time the Color method was called... but I still get the same RuntimeError. So that doesn't tell me much other than I am still looking for what hidden import I need to include for this thing to work.
Also note that I get the exact same error with both PyInstaller and cx_Freeze, in case that helps...
Edit 2
I have now tried it using anaconda for python 2.7, SAME EXACT ISSUE..... I'm starting to believe the universe doesn't want this to happen. Is there somewhere else I should bring this issue up?? I have posted on the traitsui GitHub but that wasn't very helpful. This seems to be bigger than pyinstaller/cx_freeze since it happens in both....
I dealt with the same problem and finally switched to cx_freeze, which now works fine on linux and windows. The problems you are dealing with arise from statements like in the SE answer, you found, i.e. dynamic import statements, where what is imported is only determined at runtime:
be = 'pyface.ui.%s.' % tk
__import__(be + 'init')
I couldn't fix that in pyinstaller, while in cx_freeze it works, when you explicitely add the required packages in the build file. Here is the package list I used:
"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pkg_resources._vendor','pkg_resources.extern','pygments.lexers',
'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi']
Here is a full build script that works with python3.6, cx_freeze 5.0.2, mayavi 4.5.0+vtk71, traits 4.6.0, pyface 5.1.0 and traitsui 5.1.0.
import os
from cx_Freeze import setup, Executable
import cx_Freeze.hooks
def hack(finder, module):
return
cx_Freeze.hooks.load_matplotlib = hack
import scipy
import matplotlib
scipy_path = os.path.dirname(scipy.__file__) #use this if you are also using scipy in your application
build_exe_options = {"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pygments.lexers',
'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi'],
"include_files": [(str(scipy_path), "scipy"), #for scipy
(matplotlib.get_data_path(), "mpl-data"),],
"includes":['PyQt4.QtCore','PyQt4.QtGui','mayavi','PyQt4'],
'excludes':'Tkinter',
"namespace_packages": ['mayavi']
}
executables = [
Executable('main.py', targetName="main.exe",base = 'Win32GUI',)
]
setup(name='main',
version='1.0',
description='',
options = {"build_exe": build_exe_options},
executables=executables,
)
I import pyface in the following way:
os.environ['ETS_TOOLKIT'] = 'qt4'
import imp
try:
imp.find_module('PySide') # test if PySide if available
except ImportError:
os.environ['QT_API'] = 'pyqt' # signal to pyface that PyQt4 should be used
from pyface.qt import QtGui, QtCore
before importing mayavi

Matplotlib image not coming up in Visual Studio Code on Mac

I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.

Using PyInstaller or cx_Freeze produces very large file size with Anaconda2

It seems PyInstaller and cx_Freeze will include many packages with an Anaconda2 environment.
Using either will produce a file over 600MB, this is largely due to a chain of includes which eventually includes Numpy, which then includes just about everything else.
An example of the includes that cause 600MB+ worth of packages:
import sys
from sys import argv
from os import path
from Tkinter import *
import tkFileDialog
from PyQt4 import QtCore, QtGui, uic, QtOpenGL
from moviepy.editor import *
Which doesn't seem like much, but I cannot reduce the file down without explicitly excluding Numpy, but even then it's still ~140MB and won't execute.
Has anyone had experience with dealing with this situation? 600MB is well excessive for a tiny app.
This has nothing to do with Anaconda. You will get the same size when you use a standard Python installation.
The reason why your distribution is so big is because of the additional packages you need to supply. A very big chunk is PyQt4. This takes about 250MB of disk space for all the Qt Libraries and the Python bindings. Also as you already wrote numpy takes another big chunk of disk space (250MB on my computer). If you rely on these packages there is not much you can do to decrease the needed disk space.
However you can exclude packages you don't need. I discovered that cx_freeze likes to include packages that are installed in your Python environment regardless if you use them in your project or not. So I recommend to create a virtual environment that only contains the packages you need in your project. Also you may want to exclude tkinter from the build.
This issue was directly related to Anaconda.
When constructing an exe with Pyinstaller, it will get confused at Anaconda's package linkage and include obscene amounts of things.
This problem is fixed when using a non-Anaconda python with wheels for non-pip packages.

Spyder default module import list

I'm trying to set up a slightly customised version of Spyder. When Spyder starts, it automatically imports a long list of modules, including things from matplotlib, numpy, scipy etc. Is there a way to add my own modules to that list?
In case it makes a difference, I'm using the Spyder configuration provided by the Python(X,Y) Windows installer.
First you have to create a Python file with the modules you want to import at startup. Suppose you call it my_imports.py and that it has this contents:
import numpy as np
import matplotlib.pyplot as plt
Then you have to go to
Tools > Preferences > IPython Console > Startup > Run a file
select the option
Use the following file
and finally click on the button to the right of text field below that option to select your my_imports.py file.
The startup script for Spyder is in site-packages/spyderlib/scientific_startup.py.
Carlos' answer would also work, but this is what I was looking for.
If Spyder is executed as a python script by python binary, then you should be able to simply edit Spyder python sources and include the modules you need. You should take a look into how is it actually executed upon start.

Categories