首页 > 代码库 > Py2exe 打包 Python项目(traits、traitsui、matplotlib)

Py2exe 打包 Python项目(traits、traitsui、matplotlib)

这段时间做了用Python做了一个科学计算的项目,项目中用到了很多的第三方Python库,包括PyQt、traits、traitsui、matplotlib、pyface、table、numpy、tvtk等等。Python打包traits和traitsui很麻烦,选用了py2exe、bbfreeze等打包工具,最后在google的帮助下使用了py2exe解决了问题。

其主要难点是要知道你的项目中使用到的python库,然后在includes列表中添加这些Python库,以便能正确打包项目。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe
import os
import glob

 
includes = []
includes.append("sip")
includes.append('numpy')
includes.append('numpy.core')

includes.append('pygments')
includes.append('pygments.*')
includes.append('pygments.filters')
includes.append('pygments.filters.*')
includes.append('pygments.formatters')
includes.append('pygments.formatters.*')
includes.append('pygments.lexers')
includes.append('pygments.lexers.*')
includes.append('pygments.styles')
includes.append('pygments.styles.*')

includes.append('scipy')
includes.append('xml')

includes.append('lxml')
includes.append('lxml.*')
 
includes.append('wx')
includes.append('wx.*')

includes.append('traits')
includes.append('traits.etsconfig.api.*')
includes.append('traits.etsconfig')
includes.append('traits.etsconfig.*')

includes.append('traitsui')
includes.append('traitsui.*')

includes.append('traitsui.qt4')
includes.append('traitsui.qt4.*')
includes.append('traitsui.qt4.extra')
includes.append('traitsui.qt4.extra.*')
includes.append('traitsui.editors')
includes.append('traitsui.editors.*')
includes.append('traitsui.extras')
includes.append('traitsui.extras.*')
includes.append('traitsui.menu')
includes.append('traitsui.menu.*')


includes.append('pyface')
includes.append('pyface.*')
includes.append('pyface.qt')
includes.append('pyface.qt.*')

includes.append('pyface.ui.qt4')
includes.append('pyface.ui.qt4.init')
includes.append('pyface.ui.qt4.*')
includes.append('pyface.ui.qt4.code_editor.*')
includes.append('pyface.ui.qt4.console.*')
includes.append('pyface.ui.qt4.tasks.*')
includes.append('pyface.ui.qt4.action.*')
includes.append('pyface.ui.qt4.timer.*')
includes.append('pyface.ui.qt4.wizard.*')
includes.append('pyface.ui.qt4.workbench.*')

includes.append('wx')
includes.append('wx.*')

includes.append('PyQt4')
includes.append('PyQt4.*')


includes.append('enable')
includes.append('enable.drawing')
includes.append('enable.tools')
includes.append('enable.wx')
includes.append('enable.wx.*')
includes.append('enable.qt4')
includes.append('enable.qt4.*')

includes.append('enable.savage')
includes.append('enable.savage.*')
includes.append('enable.savage.svg')
includes.append('enable.savage.svg.*')
includes.append('enable.savage.svg.backends')
includes.append('enable.savage.svg.backends.wx')
includes.append('enable.savage.svg.backends.wx.*')
includes.append('enable.savage.svg.css')
includes.append('enable.savage.compliance')
includes.append('enable.savage.trait_defs')
includes.append('enable.savage.trait_defs.*')
includes.append('enable.savage.trait_defs.ui')
includes.append('enable.savage.trait_defs.ui.*')
includes.append('enable.savage.trait_defs.ui.qt4')
includes.append('enable.savage.trait_defs.ui.qt4.*')

includes.append('tvtk')
includes.append('tvtk.*')

includes.append('tvtk.custom')
includes.append('tvtk.custom.*')
includes.append('tvtk.pipeline')
includes.append('tvtk.pipeline.*')
includes.append('tvtk.plugins')
includes.append('tvtk.plugins.*')
includes.append('tvtk.pyface')
includes.append('tvtk.pyface.*')
includes.append('tvtk.pyface.ui')
includes.append('tvtk.pyface.ui.*')
includes.append('tvtk.pyface.ui.qt4')
includes.append('tvtk.pyface.ui.qt4.*')
includes.append('tvtk.tools')
includes.append('tvtk.tools.*')
includes.append('tvtk.util')
includes.append('tvtk.util.*')
includes.append('tvtk.view')
includes.append('tvtk.view.*')

includes.append('matplotlib.backends')
includes.append('matplotlib.backends.backend_qt4agg')
includes.append('matplotlib.figure')

includes.append('tables')
includes.append('tables.*')

includes.append('apptools.preferences')
includes.append('apptools.preferences.*')
includes.append('apptools.preferences.ui')
includes.append('apptools.preferences.ui.*')

packages = []
 
data_folders = []

# Traited apps:
ETS_folder = r'C:\Python27\Lib\site-packages'

data_folders.append( ( os.path.join(ETS_folder,r'enable\images'), r'enable\images') ) 
data_folders.append( ( os.path.join(ETS_folder,r'traitsui\qt4\images'), r'traitsui\qt4\images') )
data_folders.append( ( os.path.join(ETS_folder,r'tvtk\pyface\images\16x16'), r'tvtk\pyface\images\16x16') )
data_folders.append( ( os.path.join(ETS_folder,r'pyface\images'), r'pyface\images') )
data_folders.append( ( os.path.join(ETS_folder,r'pyface\ui\qt4\images'), r'pyface\ui\qt4\images') )
data_folders.append( ( os.path.join(ETS_folder,r'pyface\ui\qt4\workbench\images'), r'pyface\ui\qt4\workbench\images') )

data_folders.append( ( os.path.join(ETS_folder,r'tvtk'), r'tvtk') )
data_folders.append( ( os.path.join(ETS_folder,r'tvtk\plugins\scene'), r'tvtk\plugins\scene') )

data_folders.append( ( os.path.join(ETS_folder,r'mayavi\preferences'), r'mayavi\preferences') )
data_folders.append( ( os.path.join(ETS_folder,r'mayavi\core\lut'), r'mayavi\core\lut') )
data_folders.append( ( os.path.join(ETS_folder,r'mayavi\core\images'), r'mayavi\core\images') )
data_folders.append( ( os.path.join(ETS_folder,r'mayavi\core\ui\images'), r'mayavi\core\ui\images') )

data_folders.append( ( os.path.join(ETS_folder,r'traitsui\image\library'), r'traitsui\image\library') )

data_folders.append( ( os.path.join(ETS_folder,r'PyQt4\plugins\imageformats'), r'imageformats'))

data_folders.append( ( os.path.join(ETS_folder,r'matplotlib\mpl-data\images'), r'\matplotlib\mpl-data\images') )

rotor_resources_folder = 'C:\Users\cloud\workspace\Rotor3\src'
data_folders.append( ( os.path.join(rotor_resources_folder,r'resources'), r'resources') )
data_folders.append( ( os.path.join(rotor_resources_folder,r'resources\file'), r'resources\file') )
data_folders.append( ( os.path.join(rotor_resources_folder,r'resources\folder'), r'resources\folder') )
data_folders.append( ( os.path.join(rotor_resources_folder,r'resources\toolbar'), r'resources\toolbar') )
data_folders.append( ( os.path.join(rotor_resources_folder,r'resources\workspace'), r'resources\workspace') )




# Matplotlib
import matplotlib as mpl
data_files = mpl.get_py2exe_datafiles()
 
# Parsing folders and building the data_files table
for folder, relative_path in data_folders:
    for file in os.listdir(folder):
        f1 = os.path.join(folder,file) 
        if os.path.isfile(f1): # skip directories
            f2 = relative_path, [f1]
            data_files.append(f2)


setup(windows=[{"script":'run2.py',"icon_resources":[(1,"xxx.ico")]}],
    author="Hangzhou Dianzi University",
    version = "1.0",
    description = "DPT For Hangzhou Dianzi University",
    name = "Hangzhou Dianzi University",
    options = {"py2exe": {    "optimize": 0,
                              "packages": packages,
                              "includes": includes,
                              "dll_excludes": ["MSVCP90.dll", "w9xpopen.exe"],
                              "dist_dir": 'dist',
                              "bundle_files":2,
                              "xref": False,
                              "skip_archive": True,
                              "ascii": False,
                              "custom_boot_script": '',
                              "compressed":False,
                             },},
    data_files=data_files)

Py2exe 打包 Python项目(traits、traitsui、matplotlib)