Pyinstaller,tensorflow.contrib运行时的延迟加载程序错误

时间:2018-05-31 16:32:15

标签: python tensorflow pyinstaller

在python 3.5 32bit中使用3.3.1 pyinstaller,我将.py应用程序转换为.exe

应用程序使用张量流并在某些时候抛出

Traceback (most recent call last):
  File "face_classify.py", line 140, in <module>
    loaded_model = load_c3d.c3d_model_obj(user_case)
  File "load_c3d.py", line 80, in __init__
    'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005),
  File "load_c3d.py", line 47, in _variable_with_weight_decay
    var = _variable_on_cpu(name, shape, tf.contrib.layers.xavier_initializer())
  File "site-packages\tensorflow\__init__.py", line 35, in __getattr__
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'tensorflow.contrib'

运行.exe时会发生这种情况。看起来像文件路径问题。 tensorflow的__init__.py执行此操作

# Lazily import the `tf.contrib` module. This avoids loading all of the
# dependencies of `tf.contrib` at `import tensorflow` time.
  def __getattr__(self, item):
    global contrib
    # Replace the lazy loader with the imported module itself.
    import importlib  # pylint: disable=g-import-not-at-top
    contrib = importlib.import_module('tensorflow.contrib')
    return getattr(contrib, item)

对我而言,pyinstaller无法跟踪这个延迟加载。但是contrib文件夹正确地放在我的python安装中:C:\Python35\Lib\site-packages\tensorflow\contrib我该如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

在我的情况下,您可以在.spec文件中的hiddenimport中添加'tensorflow.contrib'。但是,我在修复导入后遇到了另一个问题,但你可以先尝试这个。

答案 1 :(得分:0)

您可以将“ tensorflow.contrib”添加到--hidden-import

pyinstaller -F face_classify.py --hidden-import tensorflow.contrib

答案 2 :(得分:0)

希望这对有

的人有帮助
`ModuleNotFoundError: No module named 'sklearn.*'`

`ModuleNotFoundError: No module named 'h5py.*'`

在构建pyinstaller期间或之后

如果您遇到h5py错误的示例

运行pyinstaller myscript.py后,会生成myscript.spec

走进myscript.spec

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['myscript.py'],
         binaries=None,
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=None)
# ... rest of a file untouched

添加

from PyInstaller.utils.hooks import collect_submodules

hidden_imports = collect_submodules('h5py')

hiddenimports=hidden_imports,

喜欢

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

from PyInstaller.utils.hooks import collect_submodules

hidden_imports = collect_submodules('h5py')

a = Analysis(['myscript.py'],
         binaries=None,
         datas=[],
         hiddenimports=hidden_imports,
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=None)
# ... rest of a file untouched

然后保存myscript.spec并运行命令pyinstaller myscript.spec

贷记到9dogs   Pyinstaller created exe file can not load a keras nn model

答案 3 :(得分:0)

即使pyinstaller 4.0(今天最新的稳定版本)也有此错误。 但是,如果不使用--one_file,则有一种解决方法。 使用--hiddenimports =“ tensorflow.contrib”编译程序,然后将所有.so从tensorflow.contrib复制到可执行文件查找它们的位置。因此,您需要在tensorflow内部创建contrib文件夹,然后复制库。

mkdir path_to_dist/app_name/tensorflow/contrib
cd /usr/local/lib/python3.6/dist-packages/tensorflow/contrib && find ./ -name *.so |xargs -i cp --parents {} path_to_dist/app_name/tensorflow/contrib/

如果不使用python 3.6,请更改python版本。

我不知道如何使用--one_file。

来自github的andrewjong和aodiwei的版权。解决方法是关于this的问题。