py2exe 打包 PyQt4 的问题

本文详细介绍了在使用py2exe进行打包时遇到的问题及其解决方法,包括但不限于PyQt4与Qt、VS2008运行库、lxml、WebKit、twisted等组件的相关注意事项。文章提供了一个具体的setup.py示例,帮助开发者避免常见错误,顺利打包并运行程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.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
) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值