|
我正在嘗試使用Tensorflow和Pyinstaller制作我的Python代碼的可執(zhí)行文件.可執(zhí)行文件生成正確,但當(dāng)我嘗試運(yùn)行它時(shí),我收到以下錯(cuò)誤:
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
如果仔細(xì)觀察,Pyinstaller期望文件_checkpoint_ops.so在目錄tensorflow / contrib / util / tensorflow / contrib / framework / python / ops /中,但是沒(méi)有這樣的目錄. _checkpoint_ops.so位于tensorflow / contrib / framework / python / ops /.如何糾正這個(gè)錯(cuò)誤? 解決方法: 將以下內(nèi)容添加到spec文件中(找到tensorflow二進(jìn)制文件并將它們添加到主二進(jìn)制文件目錄中的.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, '.'))
確保還將二進(jìn)制文件添加到Analysis
a = Analysis(...,
binaries=tensorflow_binaries,
...)
并確保.exe / .app版本中包含二進(jìn)制文件.這是一個(gè)簡(jiǎn)單的例子:
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'
},
)
您需要做的最后一件事是修改tensorflow load_library.py文件以指向.app運(yùn)行時(shí)期間二進(jìn)制文件的相對(duì)路徑.
該文件通常位于此處(相對(duì)于python安裝):site-packages / tensorflow / python / framework / load_library.py
您需要添加resource_path函數(shù)并修改load_op_library函數(shù)以將file_name指向相對(duì)路徑(并忽略按子文件夾查找二進(jìn)制文件).
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 of project item (ex: models/bladder...h5)
Returns
-------
path : str
path relative to .exe 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])
這將使tensorflow通過(guò)pyinstaller應(yīng)用程序的相對(duì)路徑獲取二進(jìn)制文件(參見(jiàn):https:///PyInstaller/runtime-information.html#run-time-information).
然后,Tensorflow將獲取由.spec文件打包的二進(jìn)制文件.
記得在Pyinstaller打包后取消注釋load_library.py中的library_filename = resource_path(library_filename.split(‘/’)[ – 1])行(否則Tensorflow在你的本地Python環(huán)境中運(yùn)行時(shí)將無(wú)法找到二進(jìn)制文件! ) 來(lái)源:https://www./content-1-315601.html
|