py2exe 打包注意事项
- 开发环境中只有 PyQt4, 不要安装 Qt 的开发包或者 PySide, 否则打包的 dll 可能会拷贝这两个包的 DLL 而导致程序不能加载运行.
- 注意要拷贝 VS 2008 的运行库.
- 注意要手动拷贝 PyQt4 目录下的 Qt 的 Plugins 目录. 否则 WebKit 不能显示图片文件或者 Phonon 不能播放声音.
- py2exe 打包时不要使用
bundle_files
打包成一个文件, 可能程序运行出现错误. - 找不到
MSVCP90.dll
的话, 在sys.path
中添加路径. - lxml包打包时, 需要在 py2exe 的
options
的packages
中包含, 如果在includes
中包含, 会出现_elementpath
无法 import 的问题. - 如果包含了 WebKit, 还需要在 py2exe
options
的includes
部分包含QNetwork
, 否则 WebKit 不能运行. - 如果包含了 twisted, 由于 twisted 依赖
zope.interface
, 需要包含zope.interface
, 而zope.interface
包名不是标准的 Python 模块名, 导致 py2exe 找不到zope
模块. 解决办法是直接修改系统文件, 在 Python 的 site-packages/zope 目录下创建空的__init__.py
文件. :) 比较 dirty. - 如果使用了 email 模块, 需要在
packages
中包含email
模块. - 如果目标机器不安装 VC 的运行库, 需要自己拷贝 VS 2008 的运行库.
- 即使自己拷贝 VS2008 的运行库, Qt 的 Plugin (包括图片, phonon) 也不会正确加载, 一切的罪魁祸首是 xp 的 embedded manifest 文件,这个问题困惑了我好久! QtWebKit 不能显示jpeg, gif 等图片, phonon 不能发声, 不能显示视频等. 解决办法是: 由于使用自己拷贝的 VS2008 运行库, 需要在每个 plugin 的目录下拷贝一份, 包括
imageformats
和phonon_backend
.
源代码例子如下:
# FILE: setup.py
import sys
from glob import glob
from distutils.core import setup
sys.path.append(r'c:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT')
data_files = ['zh_CN.qm', 'admailer.ico', 'adview.html', 'adview.xls']
console = [{'script': 'main.py',
'icon_resources' : [(1, 'admailer.ico')],
'dest_base' : 'avmailer',
}]
options_py2exe = {
'py2exe' : {
'packages' : ['lxml', 'email'],
'includes': [
'sip', 'twisted', 'xlrd', 'htmllaundry', 'lxml','zope.interface',
'sqlite3','gzip', 'PyQt4.QtNetwork'],
#'dll_excludes' : ['MSVCP90.dll'],
'bundle_files' : 3,
'optimize': 2,
}
}
if sys.platform == 'win32':
import py2exe
data_files += [
('Microsoft.VC90.CRT', glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*')),
('imageformats', glob(r'C:\Python27\Lib\site-packages\PyQt4\plugins\imageformats\*.*')),
(r'imageformats\Microsoft.VC90.CRT', glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*')),
]
import py2exe
setup (
name='admailer',
version='0.9',
packages=[''],
url='http://www.adview.cn',
license='MIT',
author='kerberos',
author_email='imkerberos@yahoo.com.cn',
description='',
options = options_py2exe,
console = console,
data_files = data_files
)