使用 cx_freeze 4.3.1 冻结 h5py 2.4 时出错



我正在尝试用cx_Freeze冻结一个Python脚本。该脚本使用 h5py v2.4.0b1。当我运行cx_Freeze创建的可执行文件时,我得到以下回溯:

Traceback (most recent call last):
  File "C:Python34libsite-packagescx_FreezeinitscriptsConsole.py", line 27, in <module>
    exec(code, m.__dict__)
  File "main.py", line 7, in <module>
    from xman import xmanLogging, xmanManager, xmanInfo
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "d:useraagbdsProgrammierungPythonEclipseProjectsxMANsrcxman__init__.py", line 3, in <
module>
    from .xmlHandler import barMAN, xmlClass
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "d:useraagbdsProgrammierungPythonEclipseProjectsxMANsrcxmanxmlHandler__init__.py", l
ine 7, in <module>
    from . import barMAN, xmlClass
  File "X:Python34-x32libimportlib_bootstrap.py", line 2284, in _handle_fromlist
  File "X:Python34-x32libimportlib_bootstrap.py", line 321, in _call_with_frames_removed
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "d:useraagbdsProgrammierungPythonEclipseProjectsxMANsrcxmanxmlHandlerxmlClass.py", l
ine 25, in <module>
    from xman.COMInterface import COMmanager
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "d:useraagbdsProgrammierungPythonEclipseProjectsxMANsrcxmanCOMInterface__init__.py",
 line 7, in <module>
    from . import COMmanager
  File "X:Python34-x32libimportlib_bootstrap.py", line 2284, in _handle_fromlist
  File "X:Python34-x32libimportlib_bootstrap.py", line 321, in _call_with_frames_removed
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "d:useraagbdsProgrammierungPythonEclipseProjectsxMANsrcxmanCOMInterfaceCOMmanager.py
", line 20, in <module>
    import logging, win32com.client , pythoncom, pywintypes, h5py, 
  File "X:Python34-x32libimportlib_bootstrap.py", line 2237, in _find_and_load
  File "X:Python34-x32libimportlib_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1191, in _load_unlocked
  File "X:Python34-x32libimportlib_bootstrap.py", line 1161, in _load_backward_compatible
  File "C:Python34libsite-packagesh5py__init__.py", line 10, in <module>
    from h5py import _errors
ImportError: cannot import name '_errors'

我正在使用Python 3.4.2 - 32位。有人有想法吗?

我花了两天时间试图弄清楚这个问题。

我使用的是 python 2.7 64 位,但我在 Windows、ubuntu 14 和 mac os 10.9 和 10.10 中遇到了完全相同的错误。我也在使用 h5py 2.4.0 和 cx_Freeze 4.3.4。

我的解决方案如下:

https://bitbucket.org/anthony_tuininga/cx_freeze/pull-request/64/only-add-h5pyapi_gen-if-it-can-be-imported/diff

我不得不将cx_Freeze load_h5py的 hooks.py 更改为:

def load_h5py(finder, module):
   """h5py module has a number of implicit imports"""
   finder.IncludeModule('h5py.defs')
   finder.IncludeModule('h5py.utils')
   finder.IncludeModule('h5py._proxy')
   try:
      finder.IncludeModule('h5py._errors')
      finder.IncludeModule('h5py.h5ac')
   except:
      pass
   try:
      finder.IncludeModule('h5py.api_gen')
   except:
      pass

此外,似乎 h5py.*.so 的一些引用依赖项与

@loader_path/.dylibs/libhdf5.8.dylib

这不会被cx_freeze重写。

我不得不在cx_freeze中修改代码(实际上,我将代码复制到我的 setup.py 并在主 setup(....) 调用后立即调用它以发布处理构建中的所有文件)

    # this is a straight copy paste from macdist.py in the cx_Freeze code
def setRelativeReferencePaths(binDir):
    """ For all files in Contents/MacOS, check if they are binaries
        with references to other files in that dir. If so, make those
        references relative. The appropriate commands are applied to all
        files; they will just fail for files on which they do not apply."""
    files = []
    for root, dirs, dir_files in os.walk(binDir):
        files.extend([os.path.join(root, f).replace(binDir + "/", "")
                      for f in dir_files])
    for fileName in files:
        # install_name_tool can't handle zip files or directories
        filePath = os.path.join(binDir, fileName)
        if fileName.endswith('.zip'):
            continue
        # ensure write permissions
        mode = os.stat(filePath).st_mode
        if not (mode & stat.S_IWUSR):
            os.chmod(filePath, mode | stat.S_IWUSR)
        # let the file itself know its place
        subprocess.call(('install_name_tool', '-id',
                         '@executable_path/' + fileName, filePath))
        # find the references: call otool -L on the file
        otool = subprocess.Popen(('otool', '-L', filePath),
                                 stdout=subprocess.PIPE)
        references = otool.stdout.readlines()[1:]
        for reference in references:
            # find the actual referenced file name
            referencedFile = reference.decode().strip().split()[0]
            if referencedFile.startswith('@loader_path/.dylibs/'):
                # this file is likely an hdf5 file
                print "Found hdf5 file {} referencing {}".format(filePath, referencedFile)
            elif referencedFile.startswith('@'):
                # the referencedFile is already a relative path
                continue
            path, name = os.path.split(referencedFile)
            # some referenced files have not previously been copied to the
            # executable directory - the assumption is that you don't need
            # to copy anything fro /usr or /System, just from folders like
            # /opt this fix should probably be elsewhere though
            # if (name not in files and not path.startswith('/usr') and not
            #        path.startswith('/System')):
            #    print(referencedFile)
            #    self.copy_file(referencedFile,
            #                   os.path.join(self.binDir, name))
            #    files.append(name)
            # see if we provide the referenced file;
            # if so, change the reference
            if name in files:
                newReference = '@executable_path/' + name
                subprocess.call(('install_name_tool', '-change',
                                 referencedFile, newReference, filePath))