py2exe setup.py 范例,解决打包后XP样式丢失

我们在用py2exe打包Python程序时,经常会碰到各种各样的问题,
如XP样式问题、设置图标问题,"LookupError: unknown encoding: utf-8"问题等等。
经过一番整理如下,记下来防止以后忘记:
from distutils.core import setup
import py2exe
import sys
class Target:
def __init__(self, **kw):
self.__dict__.update(kw)
# for the versioninfo resources
self.version = "0.2.2"
self.company_name = "your company"
self.copyright = "your copyright"
self.name = "Name about this program's description"
#Put content in *.exe.manifest to here, the key to resolve XP sytle after pack
manifest_template = '''
%(prog)s Program



'''
RT_MANIFEST = 24
#detail setting about the target program.
SyncList = Target(
# The first three parameters are not required, if at least a
# 'version' is given, then a versioninfo resource is built from
# them and added to the executables.
version = "0.21",
description = "Your description about this program",
name = "Your program's name",
# what to build, script equals your program's file name
script = "SyncList.py",
other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="SyncList"))],
#icon.ico is the target program's icon
icon_resources = [(1, "icon.ico")],
#target exe file name is SyncList here
dest_base = "SyncList")
#deal with some case after packing, run with error "LookupError: unknown encoding: utf-8"
includes = ["encodings",
"encodings.*"]
excludes = []
setup(
options = {"py2exe": {"typelibs":
# typelib for WMI
[('{565783C6-CB41-11D1-8B02-00600806D9B6}', 0, 1, 2)],
# create a compressed zip archive
"compressed": 1,
"optimize": 2,
"ascii": 1,
"bundle_files": 1,
"includes": includes,
"excludes": excludes}},
# The lib directory contains everything except the executables and the python dll.
# Can include a subdirectory name.
zipfile = None,
#remove the DOS window when run the program, replace "console" with "windows"
windows = [SyncList],
#all file list below will be include in the pack folder.
data_files=[("",
["icon", ]),]
)