Python PyInstaller 4.0 packaging TensorFlow 2.0 project not working ImportError: cannot import name 'pywrap_tensorflow'

Viewed 4025

I am trying to bundle a python application using pyinstaller that uses tensorflow.

I am now using Pyinstaller-4.0.dev0+2f4426f52, Tensorflow 2.0, Keras 2.3 and Python 3.7.3 all in a virtual environment.

I have tried various older versions, but each older version had a different issue that I could not resolve. I have dedicated 3 days to trying to solve this problem.

A simplified example of the script I am running is:

from tensorflow import keras
model = keras.Sequential()

I used the following command to run pyinstaller:

pyinstaller --noconfirm --log-level=DEBUG ^
    --onedir ^
    --clean ^
    --name MyModel ^
    --hidden-import=tensorflow_core ^
    --noupx ^
    main.py

Using hidden-import appears to be redundant, as the output states:

DEBUG: Hidden import 'tensorflow_core' already found

There were no build errors or warnings in the log.

I have looked into the pyz-00.pyz file and can confirm that TensorFlow modules are included in the file. I confirmed that the archive contains:

'tensorflow_core.python': (1, 9871434, 2355),

However when I try to run the exe I get the following error:

File "site-packages\tensorflow_core\python_init_.py", line 49, in ImportError: cannot import name 'pywrap_tensorflow' from 'tensorflow_core.python' (\dist\MyModel\tensorflow_core\python_init_.pyc)

It appears that pyinstaller can't deal with tensorflow, and I wondered whether anyone with experience here might know how to write a hook for it or knows of some other workaround?

2 Answers

Can you try changing the directory from which you are invoking this program? Sometimes errors like that come up when the current directory has a tensorflow subdirectory with an init.py in it, which Python will prefer to any other installations. You can also refer to below link for some more troubleshooting steps:

Tensorflow-gpu with pyinstaller

Go to this path /usr/local/lib/python3.7/dist-packages
Copy the tensorflow_core folder, paste it to your Project Directory and rename it as:

tensorflow from tensorflow import keras

After import tensorflow as tf, it works for me. Please, check.

Related