ImportError: cannot import name 'pywrap_tensorflow'

Viewed 2755

I was trying to make a .py to .exe file using pyinstaller. After building the exe file, when I tried to run the .exe file through cmd, then it shows this error bellow.

D:\Ne folder\New folder\GuiExe>GuiExe.exe
Using TensorFlow backend.
Traceback (most recent call last):
  File "GuiExe.py", line 4, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\__init__.py", line 3, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\utils\__init__.py", line 6, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\backend\__init__.py", line 1, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\backend\load_backend.py", line 90, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\tensorflow\__init__.py", line 101, in <module>
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
  File "lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
  File "lib\site-packages\tensorflow\__init__.py", line 44, in _load
  File "importlib\__init__.py", line 126, in import_module
  File "c:\users\manash\tesorflow\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
ImportError: cannot import name 'pywrap_tensorflow'
[1764] Failed to execute script GuiExe

P.s. I also tied --hidden-import pywrap_tensorflow and --paths where pywrap_tensorflow.py file situated.

My PY to EXE command was

pyinstaller --paths C:/Users/Manash/tesorflow/Lib/site-packages/tensorboard/compat/tensorflow_stub/pywrap_tensorflow.py --hidden-import pkg_resources.py2_warn --hidden-import pywrap_tensorflow --add-data "C:/Ne folder/train_with_vali_test_model_6_111(GLOVES)_soreo-ou_test.pckle";. GuiExe.py

My python version# 3.5.4 Tensorflow version# 2.1.0 keras version# 2.3.1

Please, help me, and tell me should I give more information, or Which ones information should I provide. To get a solution. Thanks in advance.

2 Answers

You can try the solution stated here.

As per the discussion:

Step 1: Create a directory structure like this:

- main.py  # Your code goes here
- hooks
   - hook-tensorflow.py

Step 2: Copy the following into hook-tensorflow.py:

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)

Step 3: When compiling, add the command line option --additional-hooks-dir=hooks

Step 4: Simply add the full import name into the packages list.

The main.py file contains the following code from tensorflow import *

I have solved this error by changing my Python(3.7 to 3.5), tensorflow(2 to 1.15) version.

Related