py2exe 打包 PyQt4 的问题

来源:互联网 发布:java处理高并发问题 编辑:程序博客网 时间:2024/05/17 14:15

py2exe 打包注意事项

  • 开发环境中只有 PyQt4, 不要安装 Qt 的开发包或者 PySide, 否则打包的 dll 可能会拷贝这两个包的 DLL 而导致程序不能加载运行.
  • 注意要拷贝 VS 2008 的运行库.
  • 注意要手动拷贝 PyQt4 目录下的 Qt 的 Plugins 目录. 否则 WebKit 不能显示图片文件或者 Phonon 不能播放声音.
  • py2exe 打包时不要使用 bundle_files 打包成一个文件, 可能程序运行出现错误.
  • 找不到 MSVCP90.dll 的话, 在 sys.path 中添加路径.
  • lxml包打包时, 需要在 py2exe 的 optionspackages 中包含, 如果在 includes 中包含, 会出现 _elementpath 无法 import 的问题.
  • 如果包含了 WebKit, 还需要在 py2exe optionsincludes 部分包含 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 的目录下拷贝一份, 包括imageformatsphonon_backend.

源代码例子如下:

# FILE: setup.pyimport sysfrom glob import globfrom distutils.core import setupsys.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 py2exesetup (    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) 
原创粉丝点击