Packaging a python application ( with enthought, matplotlib, wxpython) into executable - python

My Python 2.7 application uses matplotlib, enthought (mayavi, traits), wxpython libraries. I need to package it into an executable on Windows, which, after some research and experimenting seems like a not straightforward task.
I have so far experimented with PyInstaller and bbfreeze. In both of them I specify hidden imports/includes (which I could gather fromrandom information on the web) to import the Enthought packeges. Both manage to create an executable (for bbfreeze I excluded the matplotlib part of my application so far), but when I run it, both return the same error:
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 128, in <module>
File "__main__test__.py", line 23, in <module>
File "traitsui/api.py", line 36, in <module>
File "traitsui/editors/__init__.py", line 23, in <module>
File "traitsui/editors/api.py", line 49, in <module>
File "traitsui/editors/table_editor.py", line 37, in <module>
File "traitsui/table_filter.py", line 35, in <module>
File "traitsui/menu.py", line 128, in <module>
File "pyface/toolkit.py", line 98, in __init__
NotImplementedError: the wx pyface backend doesn't implement MenuManager
Any ideas what should I do? Alternatively has anyone had experience with creating such an executable and can recommend a tool or method ? So far I have seen only this tutorial but it uses py2exe and apparently requires downloading the whole ETS - if nothing else gonna give it a try...

Here is the solution which worked for me.
I have tried to use bbfreeze, PyInstaller , py2exe and cx_Freeze. In the end I decided to go with cx_Freeze as it is apparently popular with people who are packaging the applications with enthought classes.
With cx_Freeze I got the similar error message as above. The problem is that it saves the necessary modules in "library.zip" file, which is something that enthought classes including mayavi have problem with. Fortunately cx_Freeze allows specifying "create_shared_zip": False option, which then makes it so that source files are directly copied into the build directory as they are, instead of in a zip file.
Additionally, I have found that some Enthought files and folders have to be manually included in include_files (same goes for scipy, source: here). After this it worked. I add my setup file code below, hope it helps.
import sys
import os
from cx_Freeze import setup, Executable
import scipy
scipy_path = os.path.dirname(scipy.__file__) #use this if you are also using scipy in your application
build_exe_options = {"packages": ["pyface.ui.wx", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_tkagg"],
"excludes": ['numarray', 'IPython'],
"include_files": [("C:\\Python27\\Lib\\site-packages\\tvtk\\pyface\\images\\", "tvtk\\pyface\\images"),
("C:\\Python27\\Lib\\site-packages\\pyface\\images\\", "pyface\\images"),
("C:\\Python27\\Lib\\site-packages\\tvtk\\plugins\\scene\\preferences.ini", "tvtk\\plugins\\scene\\preferences.ini"),
("C:\\Python27\\Lib\\site-packages\\tvtk\\tvtk_classes.zip", "tvtk\\tvtk_classes.zip"),
("C:\\Python27\\Lib\\site-packages\\mayavi\\core\\lut\\pylab_luts.pkl","mayavi\\core\\lut\\pylab_luts.pkl"),
("C:\\Python27\\Lib\\site-packages\\mayavi\\preferences\\preferences.ini","mayavi\\preferences\\preferences.ini"),
("C:\\Python27\\Lib\\site-packages\\numpy\\core\\libifcoremd.dll","numpy\\core\\libifcoremd.dll"),
("C:\\Python27\\Lib\\site-packages\\numpy\\core\\libmmd.dll","numpy\\core\\libmmd.dll"),
(str(scipy_path), "scipy") #for scipy
]
,"create_shared_zip": False #to avoid creating library.zip
}
executables = [
Executable('myfile.py', targetName="myfile.exe", base=None)
]
setup(name='myfile',
version='1.0',
description='myfile',
options = {"build_exe": build_exe_options},
executables=executables
)
Configuration:
python 2.7
altgraph==0.9
apptools==4.3.0
bbfreeze==1.1.3
bbfreeze-loader==1.1.0
configobj==5.0.6
cx-Freeze==4.3.3
Cython==0.23.4
matplotlib==1.4.3
mayavi==4.4.3
MySQL-python==1.2.5
natgrid==0.2.1
numpy==1.10.0b1
opencv-python==2.4.12
pandas==0.16.2
pefile==1.2.10.post114
Pillow==3.1.1
plyfile==0.4
psutil==4.1.0
pyface==5.0.0
Pygments==2.0.2
pygobject==2.28.6
pygtk==2.22.0
PyInstaller==3.1
pyparsing==2.0.3
pypiwin32==219
PySide==1.2.2
python-dateutil==2.4.2
pytz==2015.4
scipy==0.16.0
six==1.9.0
subprocess32==3.2.7
traits==4.5.0
traitsui==5.0.0
transforms3d==0.2.1
VTK==6.2.0

Related

cx_Freeze TypeError dist must be a Distribution instance

I am not finding a specific topic on this issue with the setup file for cx_Freeze.
I am trying to create an exe for my program but something is not wright with the distutils. I am not able to locate an update whl for this library so I am not sure if there is a known fix for this.
The program works fine without error.
Does anyone know why this issue exist.
Please note I am not able to use pip from inside my work network so I have to do everything with whl, tar.gz' and egg files to install libraries.
This is why I am trying to find an updated whl file for distutils.
My setup.py file.
from cx_Freeze import setup, Executable
base = None
build_exe_options = {'packages': ['idna',
'json',
'tkinter',
'operator',
'clipboard',
'matplotlib',
'tkinter.ttk ',
'matplotlib.pyplot',
'matplotlib.backends.backend_tkagg'],
'include_files': ['tracker1.json', 'tracker2.json']}
setup(
name='<NAME>',
options={'build.exe': build_exe_options},
version='<0.2>',
description='<some random desc>',
executables=[Executable('MAIN.py', base=base)]
)
The error:
"C:\Users\user_name\Desktop\Python 3.6.2\python.exe" "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pycharm\pycharm_setup_runner.py" "C:\Users\user_name\Desktop\Python Work Projects\GATE\setup.py"
Testing started at 2:55 PM ...
Traceback (most recent call last):
running pycharm_test
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pycharm\pycharm_setup_runner.py", line 26, in <module>
exec (fh.read(), globals(), locals())
File "<string>", line 21, in <module>
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
distutils.core.setup(**attrs)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 972, in run_command
cmd_obj = self.get_command_obj(command)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 847, in get_command_obj
cmd_obj = self.command_obj[command] = klass(self)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\setuptools\__init__.py", line 147, in __init__
_Command.__init__(self, dist)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\cmd.py", line 57, in __init__
raise TypeError("dist must be a Distribution instance")
TypeError: dist must be a Distribution instance
Try to update setuptools using e.g. setuptools‑40.8.0‑py2.py3‑none‑any.whl from Gohlke's Windows binaries See also TypeError: dist must be a Distribution instance.
After a lot of digging and dealing with several errors after I got the file to compile to an exe I fixed my issue.
The larger portion of the problem was related to the setup.py. I had to add a few things to make it all compile properly.
new setup.py file:
from cx_Freeze import setup, Executable
import os
base = "Win32GUI" # this lets the exe run without the console popping up.
# I had to add these 2 in order for tkinter to compile properly
os.environ['TCL_LIBRARY'] = r'C:\Users\user_name\Desktop\Python3.6.2\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\user_name\Desktop\Python3.6.2\tcl\tk8.6'
# eventhough numpy is not part of my main imports in my MAIN file I still needed to
# provide 'numpy.core._methods' and 'numpy.lib.format' in the packages list for
# my plot to work. I am assuming it is because `matplotlib` is using `numpy` somewhere.
build_exe_options = {'packages': ['numpy.core._methods',
'numpy.lib.format',
'idna',
'json',
'tkinter',
'operator',
'clipboard',
'matplotlib',
'tkinter.ttk',
'matplotlib.pyplot',
'matplotlib.backends.backend_tkagg'],
'include_files': [r'tracker1.json',
r'tracker2.json',
"tcl86t.dll",
"tk86t.dll"]}
# On jpeg's advice I changed build.exe to build_exe though I am not sure what the change was for.
setup(
name='<CCC>',
options={'build_exe': build_exe_options},
version='<0.2>',
description='<CCC - Copy Count Chart!.>',
executables=[Executable(r'C:\path\MAIN.py', base=base)]
)
After that I had to run the build command in the CMD or I would end up getting errors in my IDE console.
I am not sure why but it seams it is required to use the Command Prompt to rune the setup.py file or else it just wont work.
Here is the command if anyone else needs it:
python setup.py build
Keep in mind you may need to use a complete file path to work with the setup file. I had to the way my working directory was set up by using the below command:
python "C:\Users\user_name\Desktop\Python Work Projects\PROJECT\setup.py" build
I had a similar issue that was fixed by adding import setuptools to the top. I think it corrects some of the imports in cx_Freeze, but I'm not sure if it's related.
import setuptools
from cx_Freeze import setup, Executable
...
This also worked without adding 'numpy.core._methods', 'numpy.lib.format'.

Error running executable compiled with py2exe

I'm trying use py2exe to compile an eye tracking experiment written in Python 2.7 (32-bit). The experiment uses the psychopy library. I wrote the experiment using the PyCharm IDE, and the experiment runs when I run it through the PyCharm IDE, using an interpreter in a virtual environment located at C:\Users\phil\Python_2.7_32-bit.
The experiment compiles without generating any errors when I enter the following command into the command prompt: C:\Users\phil\Python_2.7_32-bit\Scripts\python.exe C:\Users\phil\PycharmProjects\iTRAC\VisSearch\setup.py py2exe.
When I run the executable generated by the above py2exe command, I get this error:
Traceback (most recent call last):
File "VisualSearch.py", line 3, in <module>
File "psychopy\__init__.pyc", line 39, in <module>
File "psychopy\preferences\__init__.pyc", line 5, in <module>
File "psychopy\preferences\preferences.pyc", line 172, in <module>
File "psychopy\preferences\preferences.pyc", line 33, in __init__
File "psychopy\preferences\preferences.pyc", line 98, in loadAll
File "psychopy\preferences\preferences.pyc", line 146, in loadAppData
File "psychopy\preferences\configobj.pyc", line 583, in __getitem__
KeyError: 'builder'
My setup.py script is as follows:
from distutils.core import setup
import py2exe
setup(windows =['C:\Users\phil\PycharmProjects\iTRAC\VisSearch\VisualSearch.py'])
I've also tried using the following setup.py script with the same results:
from distutils.core import setup
import py2exe
setup(windows = [{'script':'C:\Users\phil\PycharmProjects\iTRAC\VisSearch\VisualSearch.py',
'options' : {'py2exe':{'includes':['psychopy'],
'compressed': True,
'bundle_files': 1,}}}])
I googled the error and came up with 0 results.
Can anybody tell me why I am running into this error?
This is probably a missing config/prefs file. PsychoPy uses the configobj library to read and validate preferences but my guess is that py2exe is only automatically packaging py/pyc files and needs to include the .spec files in the psychopy/preferences folder.

How to create .exe using py2exe(or pyinstaller) on Ubuntu

Given:
- Ubuntu
- py2exe and pyinstaller
- Python script with setup.py (or else)
from distutils.core import setup
import py2exe
import os
setup(
version = "1.0",
description = 'foo',
url = "",
name = "foo",
console=[{
"script":"main.py",
"dest_base":"foo",
}],
zipfile = "shared.dll",
options = {"py2exe":{
'bundle_files': 1,
'optimize': 2,
"dll_excludes": ['MSVCP90.dll', 'msvcr71.dll', "IPHLPAPI.DLL", "NSI.dll", "WINNSI.DLL", "WTSAPI32.dll"],
"includes": ["utils"]
}}
)
Need:
- One .exe file and maybe some .dll (I realy don't know)
Steps what I did:
- setup pip3 and python 3.4 (https://askubuntu.com/questions/524399/issues-with-py2exe)
- setup py2exe for ubuntu "pip3 install py2exe"
- run "python3.4 setup.py py2exe" And got following traceback:
Traceback (most recent call last):
File "setup.py", line 2, in <module>
import py2exe
File "/usr/local/lib/python3.4/dist-packages/py2exe/__init__.py", line 9, in <module>
patch_distutils()
File "/usr/local/lib/python3.4/dist-packages/py2exe/patch_distutils.py", line 68, in patch_distutils
from . import distutils_buildexe
File "/usr/local/lib/python3.4/dist-packages/py2exe/distutils_buildexe.py", line 91, in <module>
from . import runtime
File "/usr/local/lib/python3.4/dist-packages/py2exe/runtime.py", line 3, in <module>
from .dllfinder import Scanner, pydll
File "/usr/local/lib/python3.4/dist-packages/py2exe/dllfinder.py", line 5, in <module>
from . import _wapi
File "/usr/local/lib/python3.4/dist-packages/py2exe/_wapi.py", line 4, in <module>
_kernel32 = WinDLL("kernel32")
NameError: name 'WinDLL' is not defined
- setup pyinstaller for ubuntu (https://github.com/pyinstaller/pyinstaller/wiki)
- run "pyinstaller setup.py"(same as "pyinstaller -w setup.py") and got in dist folder many files with extension .so and one file "setup" without extension
What am I doing wrong?
How can I get .exe file under Ubuntu?
Is it possible?
PS: I've read Python executables: py2exe or PyInstaller? by I didn't find answer.
You cannot use py2exe on Ubuntu or Linux in general. You cannot use it on Mac either. It is a Windows-only utility! You have to use it within Windows, whether that be in a Windows virtual machine or an actual Windows machine.
As for PyInstaller, please read the docs:
Can I use PyInstaller as a cross-compiler?
Can I package Windows binaries while running under Linux?
No, this is not supported. Please use Wine for this, PyInstaller runs fine in Wine. You may also want to have a look at this thread in the mailinglist. In version 1.4 we had build in some support for this, but it showed to work only half. It would require some Windows system on another partition and would only work for pure Python programs. As soon as you want a decent GUI (gtk, qt, wx), you would need to install Windows libraries anyhow. So it's much easier to just use Wine. - source

py2exe PackageNotFoundError

I'm currently trying to package a Tkinter app into a .exe file using py2exe. The packaging works fine, and up until a point, the program functions. When I call a certain function, though, running the .exe file logs the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "Tkinter.pyc", line 1532, in __call__
File "/Users/Gordon/Gordon's Files/AutoFormatter/lib\formatterApp.py", line 58, in go
File "formatter.pyc", line 72, in take
File "docx\api.pyc", line 25, in Document
File "docx\opc\package.pyc", line 116, in open
File "docx\opc\pkgreader.pyc", line 32, in from_file
File "docx\opc\phys_pkg.pyc", line 31, in __new__
PackageNotFoundError: Package not found at 'C:\Users\Gordon\Gordon's Files\AutoFormatter\dist\library.zip\docx\templates\default.docx'
Upon originally running py2exe, I checked the \docx\ folder and found that py2exe hadn't actually copied over the \templates\ folder. After manually unzipping the library.zip, adding in the \templates\ folder in the right place, and then manually re-zipping, however, I get the same error.
My setup.py is as follows:
from distutils.core import setup
import py2exe
setup(
windows=[{'script': 'AutoFormatter.py'}],
options={
'py2exe':
{
'includes': ['lxml.etree', 'lxml._elementpath', 'gzip', 'docx'],
}
}
)
I'm running the program on a Windows 7 computer using Python 2.7.8 and py2exe 0.6.9.
This might be too late but I have been having the same troubles as well. I don't know if python-docx was made to be compiled into a single executable yet, none the less I have found solution.
I am on pyinstaller with python2.7, essentially the same thing. I hope that you are freezing into one directory rather than one file. This won't work if you're freezing to one file
Download this here(Mediafire link)
Place it in
C:\Users\Gordon\Gordon's Files\AutoFormatter\dist\library.zip\docx\templates\default.docx
basically wherever your .exe is in.
Hopefully that does the trick
Based off of me scouring through my own directories and the docx module, when you create a document:
doc = Document()
doc.save('hello.docx')
It pulls a template for you to use, if you do not create your own, it will use the default template offered by python-docx itself.
Don't quote me on this, but I believe python-docx looks through its own directories to find the default.docx template when executing it through python.
Since we compiled the script, the path changed to the directory in which the .exe is placed, however pyinstaller (or in your case py2exe) does not include the template with the dist folder, and this creates the PackageNotFoundError

py2exe and assimulo - No module named algebraic

I am trying to build an executable using py2exe on a soft that uses the library assimulo (differential equation solver). The problem encountered is that during execution I receive:
ImportError: No module named algebraic
The exact error message is:
Traceback (most recent call last):
File "main.py", line 89, in <module>
from simulation.simulation import Simulation
File "simulation\simulation.pyc", line 18, in <module>
manages all the action linked to a simulation, like running, saving, replay, etc...
File "solver\assimuloSolver.pyc", line 7, in <module>
Explicit solver to choose in the list of assimulo solvers:
File "assimulo\solvers\__init__.pyc", line 25, in <module>
File "assimulo\solvers\kinsol.pyc", line 12, in <module>
File "assimulo\solvers\kinsol.pyc", line 10, in __load
File "kinsol.pyx", line 1, in init assimulo.solvers.kinsol (assimulo\solvers\kinsol.c:19711)
ImportError: No module named algebraic
Here qe can see that it is line 7 that produces my troubles, and this line is
from assimulo.solvers import Radau5DAE
the setup.py file for py2exe looks like the following:
from distutils.core import setup
from py2exe.build_exe import py2exe
import sys
from glob import glob
import matplotlib
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
data_files.extend(matplotlib.get_py2exe_datafiles())
sys.path.append("C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT")
excludes = ['_gtkagg', '_tkagg']
includes = [
"scipy.sparse.csgraph._validation",
"scipy.special._ufuncs_cxx",
]
opts = {
"py2exe": {
"includes":includes,
"excludes":excludes,
}
}
setup(name = "MySoft",
version = "0.1",
data_files=data_files,
windows=[{"script":"main.py"}], options=opts)
If someone has a clue, I would be very interested.
Thanks
Sometimes I have found py2exe failing to include packages, even when listed in the packages option, but have found that if I import the package in setup.py it starts to work so try adding, near the top of setup.py:
import assimulo
You will sometimes find that even
if False:
import assimulo
will work, (use this is assimulo does a lot of setting up on import).
The solution to my problem was obtained by adding, in the includes option, the algebraic package this way:
includes = ["assimulo.algebraic"]
One must also be sure that the library is added to the PATH variable. If not, one can simply add sys.path.append("path to the library"), which in my case was
sys.path.append("C:\\Python27\\Lib\\site-packages\\assimulo")
in the setup file
Thanks for the help
Cheers

Categories