将 python 应用程序(with enthought、matplotlib、wxpython)打包成可执行文件

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

我的 Python 2.7 应用程序使用 matplotlib、enthought(mayavi、traits)、wxpython 库。我需要将其打包到 Windows 上的可执行文件中,经过一些研究和试验后,这似乎不是一项简单的任务。

到目前为止,我已经尝试过 PyInstaller 和 bbfreeze。在这两个文件中,我都指定隐藏 imports/includes(我可以从网络上的随机信息中收集)来导入 Enthought 包。两者都设法创建了一个可执行文件(对于 bbfreeze,到目前为止我排除了我的应用程序的 matplotlib 部分),但是当我 运行 它时,两者 return 相同的错误:

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

有什么想法吗?或者有没有人有创建这样的可执行文件的经验并且可以推荐一种工具或方法?到目前为止,我只看到 this tutorial 但它使用 py2exe 并且显然需要下载整个 ETS - 如果没有别的可以试一试...

这是对我有用的解决方案。

我尝试过使用 bbfreeze、PyInstaller、py2exe 和 cx_Freeze。最后我决定使用 cx_Freeze,因为它显然很受那些用心 类.

打包应用程序的人的欢迎。

使用 cx_Freeze 我得到了与上面类似的错误消息。问题是它将必要的模块保存在 "library.zip" 文件中,这是 类 认为包括 mayavi 有问题的东西。幸运的是 cx_Freeze 允许指定 "create_shared_zip": False 选项,这样源文件就可以直接复制到构建目录中,而不是压缩文件中。

此外,我发现一些 Enthought 文件和文件夹必须手动包含在 include_files 中(scipy 也是如此,来源:)。在此之后它起作用了。我在下面添加了我的设置文件代码,希望对您有所帮助。

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
      ) 

配置:

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