creating standalone exe using pyinstaller with mayavi import - python

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

Related

Pyinstaller executable cannot find 'flask-compress' distribution that is included

Here is my system info:
123 INFO: PyInstaller: 4.0
123 INFO: Python: 3.5.4
124 INFO: Platform: Windows-10-10.0.18362-SP0
I've been trying to generate a Python (PyQt) executable using Pyinstaller to be used in an app. However, when I package the executable and run it, it will throw this:
pkg_resources.DistributionNotFound: The 'flask-compress' distribution was not found and is required
by the application
[14684] Failed to execute script main
This dependency already exists in my virtual environment and I have tried specifying the path to the site packages directory and the flask_compress import like this:
pyinstaller --paths C:\Users\alan9\PycharmProjects\PracticumProject\venv\Lib\site-packages --hidden-import=flask_compress main.py
Note: I have tried to create the executable for this application using different python versions, with different pyinstaller flags (onefile, windowed, onedir), on different computers with Windows 7/10, on a clean copy of a Windows 10 VM, and with fbs but I always receive the same error message:(
I solved this problem with monkey patching. Just paste this code in a module that you import before dash and you should be good to go. In my case I had flask-compress==1.5.0 so I just hardcoded the version but you could probably do something more clever.
"""
Simple module that monkey patches pkg_resources.get_distribution used by dash
to determine the version of Flask-Compress which is not available with a
flask_compress.__version__ attribute. Known to work with dash==1.16.3 and
PyInstaller==3.6.
"""
import sys
from collections import namedtuple
import pkg_resources
IS_FROZEN = hasattr(sys, '_MEIPASS')
# backup true function
_true_get_distribution = pkg_resources.get_distribution
# create small placeholder for the dash call
# _flask_compress_version = parse_version(get_distribution("flask-compress").version)
_Dist = namedtuple('_Dist', ['version'])
def _get_distribution(dist):
if IS_FROZEN and dist == 'flask-compress':
return _Dist('1.5.0')
else:
return _true_get_distribution(dist)
# monkey patch the function so it can work once frozen and pkg_resources is of
# no help
pkg_resources.get_distribution = _get_distribution
I was experiencing a similar problem and this solution was not working for me in an environment with pyinstaller==4.0, dash==1.17.0 and flask_compress==1.8.0.
However, I found that the solution proposed by Legorooj here works quite well in my case, basically instead of tensorflow one uses flask and flask_compress in the hook file.
For completeness here is how I wrote the hook-flask.py file.
from PyInstaller.utils.hooks import collect_all
def hook(hook_api):
packages = [
'flask',
'flask_compress',
'flask_caching'
]
for package in packages:
datas, binaries, hiddenimports = collect_all(package)
hook_api.add_datas(datas)
hook_api.add_binaries(binaries)
hook_api.add_imports(*hiddenimports)
Of this way no monkey patching is needed.
second update:
Since I cannot put comments yet (not enough contributions on stackoverflow?) I am adding this update to say that I tried the hook-based solution (which, in theory is the best way to deal with this sort of problem), but like Sören reported, this did not seem to get flask-compress into the distribution.
update:
The monkey-patching solution is clearly preferable, both because it leaves the flask code intact (and apparently the original functionality), but also because it (presumably) identifies the true underlying problem -- flask-compress is being brought along for the ride but simply isn't detected by pkg_resources... I wonder what (if anything) can be done to generalize this -- it may be possible to modify pkg_resources to work in a "bundled" Pyinstaller context.
original post:
I got hit with the exact same problem. Now, the "correct" thing to do is to figure out the apparently subtle way to add a proper Pyinstaller hook to take care of this issue. Maybe I will get around to doing that at some point -- certainly, what you tried to do makes sense and should've worked. I did the even more basic move of explicitly importing flask_compress in the main python script, without success.
However, since we are both trying to Pyinstallerize a flask-dependent (desktop) application, chances are very good that neither of us actually needs to gzip the responses that are being generated, so it is possible to eliminate the whole problem by "simplifying the software a bit". In my case, the references to flask_compress came from dash, so I ripped out the gzip functionality (found in Lib/site-packages/dash/dash.py). The key relevant references are on lines 21, 53, 292, 321 and 432.
"Rewire" references like those (in whatever library you use which references flask-compress) and all your problems magically disappear -- worked for me :-)
My bet is that line 53 engages machinery that is too clever/dynamic for Pyinstaller. Don't get me wrong -- Pyinstaller is nothing short of a miracle but as a community we will be forever running around trying to plug these clever-hacks because Python simply was not made to generate nice standalone executables.

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.

pandas v0.17.1 not working with py2exe

I Have a problem with python pandas v0.17.1. I upgraded from v0.16.2.
System:
Win10 x64, Python 3.4 64Bit, using PyCharm Community Edition for coding.
(numpy 1.9.3+mkl)
I'm using py2exe to create a stand-alone of a statistics program, using pandas to hold the data, matplotlib for plotting and pyqt4 for everything related to gui.
Since i upgraded pandas, the created .exe from py2exe doesn't work anymore. After doubleclick or start from commandline nothing happens. No Errors, no Errorlog file or similar, no 'window flashing' open and close again. just nothing.
I uninstalled pandas and reinstalled (fresh install) it via pip. Same problem.
I just downgraded pandas to v0.16.2 again. Everything works fine now (with v0.16.2). No other changes made.
For the sake of testing i created a simplest program as possible, only an empy pyqt mainwindow and whats needed to start the programm. works fine witout pandas. After 'import pandas' nothing happens anymore (with v0.17.1).
Somebody knows whats going on? Do i have to tweak my setup.py for the new pandas version? Because i dont get any error, i cannot check whats wrong.
main.py:
# coding=utf-8
import sys
from PyQt4 import QtCore, QtGui
import matplotlib
#import pandas
class app(QtGui.QMainWindow):
def __init__(self, *args):
QtGui.QMainWindow.__init__(self, *args)
if __name__ == "__main__":
programm = QtGui.QApplication(sys.argv)
window = app()
window.show()
eventloop = programm.exec_()
sys.exit()
setup.py:
# coding=utf-8
from distutils.core import setup
import py2exe
path_to_source = r'path to dir' # replace with your working directory
setup(
options = {"py2exe": {
'includes': ['sip'],
'excludes': [],
'optimize': 2,
'compressed' : False,
'packages': ['encodings']
#'skip_archive': True
}},
zipfile = None,
windows = [{"script": path_to_source + r"/main.py"}]
)
Just uncomment the import statement of pandas and nothing works anymore with v0.17.1.
The 'dist' directory gets created with a the same files as before.
I tried to 'include' pandas in setup.py but no effect. Dont know what to do to solve this. Are some dll's needed in the setup.py now?
Sorry for my bad english.
ps: In PyCharm, everything works fine, it's only the .exe that does not work.
ps2: Tested the same with my Win7 installation, same behavior.
I solved my problem. It was my AVAST Anti-Virus. It's 'deepscreen' feature started the programm in the background as a sandbox and analysed the .exe but never informed me about it running in the back (no info baloon etc.).
By chance, i had it deactivated while looking into Calvin's Answer.
It works on both, my PC and Laptop now, without any changes. Just deactivated AVAST 'deepscreen' feature while using the .exe created py py2exe.
I've been having the same issue as well. What I found was that a packaged called 'nbformat' caused the py2exe script to fail. I added this package into my excludes list and the script ran successfully.

import rich_ipython_widget in a py2exe excutable

I am building an executable for my python software in which I have an interactive python terminal widget. It works perfectly if I run it in python, but it fails to run if I double click the .exe I constructed with py2exe. I isolated the problem and it drops down to a simple line of code:
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
if I put this unique line in a test.py file and I run it, there is no error. now I build the .exe with py2exe using the following setup.py file:
from distutils.core import setup
from py2exe.build_exe import py2exe
setup(name = "Test",
console=[{"script":"testShell.py"}], options={})
then the resulting error message, when running the executable, is:
ImportError:
Could not load requested Qt binding. Please ensure that
PyQt4 >= 4.7 or PySide >= 1.0.3 is available,
and only one is imported per session.
Currently-imported Qt library: 'pyqt'
PyQt4 installed: False
PySide >= 1.0.3 installed: False
Tried to load: ['pyqt']
Since the app is working in python, this means that the problem is not that pyqt is not installed, but not found.
I am facing a wall here, let me know if you have a clue?
I have fixed this exact same issue by modifying the file "qt_loader.py" (in C:\Python27\Lib\site-packages\IPython\external).
What you have to do it just force the "has_binding" function to return "True".
The problems lies in the "imp.find_module" which does not work with py2exe!
Here, this is really a "false positive", (i.e. a test that fails while there is no actual problem). Skipping the test solves the issue.

No module named when using PyInstaller

I try to compile a Python project under Windows 7 using PyInstaller. The project works fine, there are no issues, however when I try to compile it the result doesn't work. Though I get no warnings during compilation there are many in the warnmain.txt file in the build directory: warnmain.txt
I don't really understand those warnings, for example "no module named numpy.pi" since numpy.pi is no module but a number. I never tried to import numpy.pi. I did import numpy and matplotlib explicitly. In addition I'm using PyQt4. I thought the error might be related to those libraries.
However I was able to compile a simple script which uses numpy succesfully:
import sys
from PyQt4 import QtGui, QtCore
import numpy as np
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.pb = QtGui.QPushButton(str(np.pi), self)
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
Successfully here means that the created executable file actually showed the desired output. However there is also a warnmain.txt file created which contains exactly the same 'warnings' as the one before. So I guess the fact that compiling my actual project does not give any success is not (or at least not only) related to those warnings. But what else could be the error then? The only output during compilation are 'INFO's and none of the is a negative statement.
I did not specify an additional hook directory but the hooks where down using the default directory as far as I could read from the compile output, e.g. hook-matplotlib was executed. I could not see any hook for numpy neither could I for my small example script but this one worked. I used the following imports in my files (not all in the same but in different ones):
import numpy as np
import matplotlib.pyplot as ppl
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from PyQt4 import QtGui, QtCore
import json
import sys
import numpy # added this one later
import matplotlib # added this one later
Since PyInstaller does not give any errors/warnings I could not figure out if the problem is related to the libraries or if there is something else to be considered.
Had a similar problem with no module named FileDialog. Discovered that with version 3.2, I could use
pyinstaller --hidden-import FileDialog ...
instead of modifying my main script.
See Listing Hidden Imports documentation
Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.
There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file.
Just change the following line:
hiddenimports=[],
to
hiddenimports=["Tkinter", "FileDialog"],
If you are getting ModuleNotFoundError: No module named ... errors and you:
call PyInstaller from a directory other than your main script's directory
use relative imports in your script
then your executable can have trouble finding the relative imports.
This can be fixed by:
calling PyInstaller from the same directory as your main script
OR removing any __init__.py files (empty __init__.py files are not required in Python 3.3+)
OR using PyInstaller's paths flag to specify a path to search for imports. E.g. if you are calling PyInstaller from a parent folder to your main script, and your script lives in subfolder, then call PyInstaller as such:
pyinstaller --paths=subfolder subfolder/script.py.
The problem were some runtime dependencies of matplotlib. So the compiling was fine while running the program threw some errors. Because the terminal closed itself immediately I didn't realize that. After redirecting stdout and stderr to a file I could see that I missed the libraries Tkinter and FileDialog. Adding two imports at the top of the main solved this problem.
I was facing the same problem and the following solution worked for me:
I first removed the virtual environment in which I was working.
Reinstalled all the modules using pip (note: this time I did not create any virtual environment).
Then I called the pyinstaller.
The .exe file created thereafter executed smoothly, without any module import error.
I had the same problem with pyinstaller 3.0 and weblib. Importing it in the main didn't help.
Upgrading to 3.1 and deleting all build files helped.
pip install --upgrade pyinstaller
If the matter is that you don't need Tkinter and friends because you are using PyQt4, then it might be best to avoid loading Tkinter etc altogether. Look into /etc/matplotlibrc and change the defaults to PyQt4, see the 'modified' lines below:
#### CONFIGURATION BEGINS HERE
# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
#modified
#backend : TkAgg
backend : Qt4Agg
# If you are using the Qt4Agg backend, you can choose here
# to use the PyQt4 bindings or the newer PySide bindings to
# the underlying Qt4 toolkit.
#modified
#backend.qt4 : PyQt4 # PyQt4 | PySide
backend.qt4 : PyQt4 # PyQt4 | PySide
May not be a good practice but installing pyinstaller in the original environment used in my project (instead of a separate venv) helped resolve ModuleNotFoundError
I had similar problem with PySimpleGUI.
The problem was, pyinstaller was installed in different directory.
SOLUTION (solved for me) : just install pyinstaller in the same directory in which the file is present (that to be converted to exe)
If these solutions don't work, simply deleting and reinstalling pyinstaller can fix this for you (as it did for me just now).
Putting this here for anyone else who might come across this post.
I had the same error. Mine said "ModuleNotFoundError: No module named 'numpy'". I fixed it by typing the following in the cmd:
pip install pyinstaller numpy

Categories