这段时间做了用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 = rC:Python27Libsite-packages
data_folders.append( ( os.path.join(ETS_folder,renableimages), renableimages) )
data_folders.append( ( os.path.join(ETS_folder,rtraitsuiqt4images), rtraitsuiqt4images) )
data_folders.append( ( os.path.join(ETS_folder,rtvtkpyfaceimages16x16), rtvtkpyfaceimages16x16) )
data_folders.append( ( os.path.join(ETS_folder,rpyfaceimages), rpyfaceimages) )
data_folders.append( ( os.path.join(ETS_folder,rpyfaceuiqt4images), rpyfaceuiqt4images) )
data_folders.append( ( os.path.join(ETS_folder,rpyfaceuiqt4workbenchimages), rpyfaceuiqt4workbenchimages) )
data_folders.append( ( os.path.join(ETS_folder,rtvtk), rtvtk) )
data_folders.append( ( os.path.join(ETS_folder,rtvtkpluginsscene), rtvtkpluginsscene) )
data_folders.append( ( os.path.join(ETS_folder,rmayavipreferences), rmayavipreferences) )
data_folders.append( ( os.path.join(ETS_folder,rmayavicorelut), rmayavicorelut) )
data_folders.append( ( os.path.join(ETS_folder,rmayavicoreimages), rmayavicoreimages) )
data_folders.append( ( os.path.join(ETS_folder,rmayavicoreuiimages), rmayavicoreuiimages) )
data_folders.append( ( os.path.join(ETS_folder,rtraitsuiimagelibrary), rtraitsuiimagelibrary) )
data_folders.append( ( os.path.join(ETS_folder,rPyQt4pluginsimageformats), rimageformats))
data_folders.append( ( os.path.join(ETS_folder,rmatplotlibmpl-dataimages), rmatplotlibmpl-dataimages) )
rotor_resources_folder = C:UserscloudworkspaceRotor3src
data_folders.append( ( os.path.join(rotor_resources_folder,rresources), rresources) )
data_folders.append( ( os.path.join(rotor_resources_folder,rresourcesfile), rresourcesfile) )
data_folders.append( ( os.path.join(rotor_resources_folder,rresourcesfolder), rresourcesfolder) )
data_folders.append( ( os.path.join(rotor_resources_folder,rresources oolbar), rresources oolbar) )
data_folders.append( ( os.path.join(rotor_resources_folder,rresourcesworkspace), rresourcesworkspace) )
# 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)