Pyinstaller,tensorflow.contrib 运行时延迟加载程序错误
Pyinstaller, tensorflow.contrib lazy loader error at runtime
使用 python 3.5 32 位中的 3.3.1 pyinstaller,我将 .py 应用程序转换为 .exe
应用程序使用 tensorflow 并在某些时候抛出
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
我该如何解决这个问题?
在我的例子中,您可以在 .spec 文件的 hiddenimport 中添加 'tensorflow.contrib'。但是,我在修复导入后遇到了另一个问题,但你至少可以先尝试一下。
您可以将“tensorflow.contrib”添加到 --hidden-import
pyinstaller -F face_classify.py --hidden-import tensorflow.contrib
希望这对有
的人有帮助
`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
即使是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 做这个。
andrewjong 和 aodiwei 的致谢来自 github。解决方案在 this 问题上。
使用 python 3.5 32 位中的 3.3.1 pyinstaller,我将 .py 应用程序转换为 .exe
应用程序使用 tensorflow 并在某些时候抛出
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
我该如何解决这个问题?
在我的例子中,您可以在 .spec 文件的 hiddenimport 中添加 'tensorflow.contrib'。但是,我在修复导入后遇到了另一个问题,但你至少可以先尝试一下。
您可以将“tensorflow.contrib”添加到 --hidden-import
pyinstaller -F face_classify.py --hidden-import tensorflow.contrib
希望这对有
的人有帮助`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
即使是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 做这个。
andrewjong 和 aodiwei 的致谢来自 github。解决方案在 this 问题上。