代码之家  ›  专栏  ›  技术社区  ›  shiftyscales

将python应用程序(带有enthough、matplotlib和wxpython)打包为可执行文件

  •  1
  • shiftyscales  · 技术社区  · 9 年前

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

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

    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-如果没有其他东西可以尝试的话。。。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Community CDub    8 年前

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

    我尝试过使用bbfreeze、PyInstaller、py2exe和cx_Freeze。最后,我决定使用cx_Freeze,因为它显然很受那些用热情的类打包应用程序的人的欢迎。

    对于cx_Freeze,我得到了与上面类似的错误消息。问题是,它将必要的模块保存在“library.zip”文件中,这是包括mayavi在内的热心类遇到的问题。幸运的是,cx_Freeze允许指定 "create_shared_zip": False 选项,这样就可以将源文件直接复制到构建目录中,而不是在zip文件中。

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

    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