Piy*_*ava 6 python pyinstaller tensorflow
我正在尝试使用Tensorflow与结合使用的Python代码的可执行文件Pyinstaller。可执行文件正确生成,但是当我尝试运行它时,出现以下错误:
Traceback (most recent call last):
File "detection_init.py", line 14, in <module>
import lib.tensorboxDetector as tensorboxDetector
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "lib/tensorboxDetector.py", line 26, in <module>
from lib.train import build_forward
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "lib/train.py", line 4, in <module>
import tensorflow.contrib.slim as slim
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/__init__.py", line 22, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/bayesflow/__init__.py", line 24, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/bayesflow/python/ops/csiszar_divergence.py", line 26, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/bayesflow/python/ops/csiszar_divergence_impl.py", line 42, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/framework/__init__.py", line 89, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/framework/python/ops/__init__.py", line 24, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 396, in load_module
exec(bytecode, module.__dict__)
File "tensorflow/contrib/framework/python/ops/checkpoint_ops.py", line 32, in <module>
File "tensorflow/contrib/util/loader.py", line 55, in load_op_library
File "tensorflow/python/framework/load_library.py", line 64, in load_op_library
tensorflow.python.framework.errors_impl.NotFoundError: tensorflow/contrib/util/tensorflow/contrib/framework/python/ops/_checkpoint_ops.so: cannot open shared object file: No such file or directory
[11241] Failed to execute script detection_init
Run Code Online (Sandbox Code Playgroud)
如果我们仔细看,Pyinstaller期望该文件_checkpoint_ops.so位于目录中,tensorflow/contrib/util/tensorflow/contrib/framework/python/ops/但是没有这样的目录。_checkpoint_ops.so位于tensorflow/contrib/framework/python/ops/。如何纠正该错误?
将以下内容添加到您的规范文件中(查找tensorflow二进制文件并将它们添加到主二进制/文件目录中的.app中):
import os
tensorflow_location = '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow'
tensorflow_binaries = []
for dir_name, sub_dir_list, fileList in os.walk(tensorflow_location):
for file in fileList:
if file.endswith(".so"):
full_file = dir_name + '/' + file
print(full_file)
tensorflow_binaries.append((full_file, '.'))
Run Code Online (Sandbox Code Playgroud)
确保您还将二进制文件添加到 Analysis
a = Analysis(...,
binaries=tensorflow_binaries,
...)
Run Code Online (Sandbox Code Playgroud)
并确保二进制文件包含在 .exe/.app 版本中。这是一个简单的例子:
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='TestApp',
debug=True,
strip=None,
upx=True,
console=True, icon='Test.icns')
app = BUNDLE(exe,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='Test.app',
icon='Test.icns',
bundle_identifier='com.Test.TestApp',
info_plist={
'NSHighResolutionCapable': 'True'
},
)
Run Code Online (Sandbox Code Playgroud)
您需要做的最后一件事是修改 TensorFlowload_library.py文件以在 .app 运行时指向二进制文件的相对路径。
该文件通常位于此处(相对于 python 安装):site-packages/tensorflow/python/framework/load_library.py
您需要添加该resource_path函数并修改该load_op_library函数以将 file_name 指向相对路径(并忽略按子文件夹查找二进制文件)。
def resource_path(relative_path):
"""Due to pyinstaller changing root dir of project filenames need to be processed in order to open properly
Parameters
----------
relative_path : str
String containing filename path
Returns
-------
path : str
path relative to this file on local computer
"""
import sys
import os
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
path = os.path.join(base_path, relative_path)
return path
def load_op_library(library_filename):
"""Loads a TensorFlow plugin, containing custom ops and kernels.
Pass "library_filename" to a platform-specific mechanism for dynamically
loading a library. The rules for determining the exact location of the
library are platform-specific and are not documented here. When the
library is loaded, ops and kernels registered in the library via the
`REGISTER_*` macros are made available in the TensorFlow process. Note
that ops with the same name as an existing op are rejected and not
registered with the process.
Args:
library_filename: Path to the plugin.
Relative or absolute filesystem path to a dynamic library file.
Returns:
A python module containing the Python wrappers for Ops defined in
the plugin.
Raises:
RuntimeError: when unable to load the library or get the python wrappers.
"""
# REMOVE AFTER PYINSTALLER USE
library_filename = resource_path(library_filename.split('/')[-1])
Run Code Online (Sandbox Code Playgroud)
这将使tensorflow通过pyinstaller应用程序的相对路径获取二进制文件(请参阅: https: //pythonhosted.org/PyInstaller/runtime-information.html#run-time-information)。
然后 Tensorflow 将获取文件打包的二进制文件.spec。
请记住取消注释Pyinstaller 打包后的library_filename = resource_path(library_filename.split('/')[-1])行load_library.py(否则 Tensorflow 在本地 Python 环境中运行时将无法找到二进制文件!)
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |