+ -
当前位置:首页 → 问答吧 → py2exe 第三方库 打包

py2exe 第三方库 打包

时间:2010-10-27

来源:互联网

我使用到 lxml

#setup.py

from distutils.core import setup
import py2exe
import sets

setup(options={'py2exe': {'bundle_files': 1}},
    zipfile=None,
    console=['test.py'],)



我下 python setup.py py2exe -p lxml


下面編出來的運行文件

D:\app\dist>test.exe
Traceback (most recent call last):
  File "test.py", line 3, in <module>
  File "zipextimporter.pyc", line 98, in load_module
  File "lxml.etree.pyx", line 48, in lxml.etree (src/lxml/lxml.etree.c:139819)
ImportError: No module named gzip


謝謝

作者: shihyu   发布时间: 2010-10-27

在你的test.py第三行,引用的模块zipextimporter.pyc
在zipextimporter.pyc的98行引入的lxml.etree.pyx48行
出错,是
ImportError: No module named gzip
没有gzip模块

一般在引入*.pyc模块时,打包不要选打成单一的EXE,这样很容易报错。
"bundle_files": 1,不要设置为1,改为2或3(不打包为单一的EXE)
见下面的例子,可能有些不能用
  1. from distutils.core import setup
  2. import py2exe
  3. import _MyCon
  4. class Target:
  5.     def __init__(self, **kw):
  6.         self.__dict__.update(kw)
  7.         self.version = _MyCon.version
  8.         self.company_name = _MyCon.company_name
  9.         self.copyright = _MyCon.copyrights
  10.         self.name = _MyCon.name_up
  11. includes = ["encodings", "encodings.*"]
  12. dll_excludes = ["w9xpopen.exe","msvcp90.dll"]
  13. excludes = ["encodings", "encodings.*","win32ui","win32gui","pywin", "pywin.debugger", "pywin.debugger.dbgcon",
  14.             "pywin.dialogs", "pywin.dialogs.list", "win32com.client"]
  15. options = {
  16.     "bundle_files": 1,
  17.     "ascii": 1,
  18.     "compressed": 1,
  19.     "optimize":2,
  20.     "dll_excludes":dll_excludes,
  21.     "includes": includes
  22.     }
  23. name_wx=Target(
  24.     script = _MyCon.script_up,
  25.     icon_resources = [(1, _MyCon.icon_up)],
  26.     dest_base = _MyCon.dest_base1
  27.     )
  28. setup(
  29.     windows = [name_wx],
  30.     options={"py2exe": options},
  31.     zipfile=None,
  32.       )
复制代码

作者: 我不是老手   发布时间: 2010-10-29