ImportError: 当使用pyinstaller创建一个.exe时,无法导入名称'pywrap_tensorflow'。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ImportError: 当使用pyinstaller创建一个.exe时,无法导入名称'pywrap_tensorflow'。相关的知识,希望对你有一定的参考价值。
我想用pyinstaller、tensorflow和tkinter创建一个.exe,我的程序在用python运行时工作正常。
我的环境是conda,python 3.6.10,Windows 10,我的tensorflow是2.1,pyinstaller 3.6。
当我创建.exe并执行它时,它返回以下错误
Traceback (most recent call last):
File "main.py", line 2, in <module>
from UI.main_ui import main_ui
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "UI\__init__.py", line 1, in <module>
from UI.main_ui import main_ui
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "UI\main_ui.py", line 6, in <module>
from Modelo.src.color_detection.classify import classify
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "Modelo\src\color_detection\classify.py", line 8, in <module>
from tensorflow.keras.models import load_model
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\tensorflow\__init__.py", line 101, in <module>
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\tensorflow_core\__init__.py", line 40, in <module>
File "site-packages\tensorflow\__init__.py", line 50, in __getattr__
File "site-packages\tensorflow\__init__.py", line 44, in _load
File "importlib\__init__.py", line 126, in import_module
File "C:\ProgramData\Anaconda3\envs\projectname\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
ImportError: cannot import name 'pywrap_tensorflow'
[9428] Failed to execute script main
我试着在additional-hooks-dir中加入一个钩子文件。
from PyInstaller.utils.hooks import collect_all
def hook(hook_api):
packages = [
'tensorflow',
'tensorflow_core',
'astor'
]
for package in packages:
datas, binaries, hiddenimports = collect_all(package)
hook_api.add_datas(datas)
hook_api.add_binaries(binaries)
hook_api.add_imports(*hiddenimports)
但没有任何变化
我的同事设法解决了这个问题。我们创建了一个.py,将一个.spec作为paremeter传递给PyInstaller。主...跑,类似的东西
PyInstaller.__main__.run([<name>.spec, --clean])
并将模块作为参数传递给.spec文件中的hiddenimportsList。
from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import collect_submodules
block_cipher = None
dataList=collect_data_files('tensorflow_core')
hiddenimportsList=['pkg_resources.py2_warn', 'tensorflow']
hiddenimportsList+=collect_submodules("tensorflow_core")
hiddenimportsList+=collect_submodules("opencv-python")
hiddenimportsList+=collect_submodules("numpy")
hiddenimportsList+=collect_submodules("scikit-image")
hiddenimportsList+=collect_submodules("imutils")
hiddenimportsList+=collect_submodules("scikit-learn")
hiddenimportsList+=collect_submodules("openpyxl")
hiddenimportsList+=collect_submodules("cv2")
hiddenimportsList+=collect_submodules("sklearn")
a = Analysis(['main.py'],
pathex=[],
binaries=[],
datas=dataList,
hiddenimports=hiddenimportsList,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name=<name>,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False, icon='<icon>.ico')
以上是关于ImportError: 当使用pyinstaller创建一个.exe时,无法导入名称'pywrap_tensorflow'。的主要内容,如果未能解决你的问题,请参考以下文章
ImportError: No module named 'xlsxwriter' - 当我使用 Pyinstaller 打包程序时
PyInstaller,规范文件,ImportError:没有名为“blah”的模块
ImportError:pyInstaller 后没有名为“tkinter”的模块
PyInstaller 与 Python:3.10.0b4 - ImportError:没有名为 _bootlocale 的模块
Pyinstaller 不工作 - ImportError: Module 'pywintypes' is not in freeze sys.path
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]