Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden import Tensorflow package not found when using Pyinstaller

I am trying to convert my object detector python project into an executable file but I always get these warnings and my executable file would not run.

64422 WARNING: Hidden import "tensorflow._api.v2.compat.v1.estimator" not found!
64425 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v2.keras.metrics" not found!
64843 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.applications.resnet50" not found!
64844 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.applications.resnet" not found!
64845 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v2.keras.backend" not found!
64857 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.estimator.tpu" not found!
64859 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v1.keras.applications.mobilenet" not found!
64892 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.applications.vgg19" not found!
64894 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v2.keras.preprocessing.text" not found!
64896 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.estimator.tpu.experimental" not found!
64899 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.applications.resnet_v2" not found!
64956 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v1.keras.wrappers.scikit_learn" not found!
64957 WARNING: Hidden import "tensorflow._api.v2.compat.v2.compat.v2.keras.applications.resnet50" not found!
64958 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.wrappers.scikit_learn" not found!
65073 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v2.keras.applications.imagenet_utils" not found!
65073 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v2.keras.datasets.cifar100" not found!
65238 WARNING: Hidden import "tensorflow._api.v2.compat.v1.compat.v1.keras.optimizers" not found!

My project structure is

- project folder
  - venv
  - main.py
  - detect.py

Inside the detect.py I have the following imports

import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

The tensorflow module can be found in the site-packages inside the venv folder

The solutions that I have tried are adding the --hidden-import tensorflow flag as said in this Question

pyinstaller --hidden-import tensorflow --onefile main.py

I have also tried this approach by creating a hooks directory with hook-tensorflow.py file

- project folder
   - venv
   - hooks
      - hook-tensorflow.py
   - main.py
   - detect.py

hook-tensorflow.py

from PyInstaller.utils.hooks import collect_all


def hook(hook_api):
    packages = [
        'tensorflow'
    ]
    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)

And then issuing this terminal command

pyinstaller --additional-hooks-dir=hooks --onefile main.py

But still, the same warning still persists and my executable file would not run.

like image 667
devios Avatar asked Jun 17 '21 23:06

devios


People also ask

Where is hidden import in PyInstaller?

To find these hidden imports, build the app with the --debug=imports flag (see Getting Python's Verbose Imports above) and run it.

Does PyInstaller support Tensorflow?

1 Answer. Show activity on this post. EDIT: The latest versions of PyInstaller (4.0+) now include support for tensorflow out of the box.

Is it possible to pack tensor flow using pyinstaller?

Package Version hey clark, I gave up on trying to pack, tensor flow using pyinstaller, it was just impossible. All the other libraries were packed but tensor flow that I downloaded from the Nvidia website made for Jetson board. It just wouldn’t pack,

How to fix “importerror no module named” error in pyinstaller?

Add additional imports to the executable using Hidden Imports. The executable needs all the imports required for the Python script to work. Pyinstaller, at times, fails to identify dynamic imports or second level imports in the Python script, and in that case, when you run the executable, you will get an error. “ImportError: No module named..."

How to include the “OS” library in pyinstaller?

For example, to include the “os” library, we can use hidden imports, as shown below. Spec file is the first file PyInstaller builds to encode the Python script file and all the options you provide at the command prompt.

How do I check which modules are installed by pyinstaller?

On each run PyInstaller writes a cross-referencing file about dependencies into the build folder: build/name/xref-name.html in the work-path= directory is an HTML file that lists the full contents of the import graph, showing which modules are imported by which ones. You can open it in any web browser.


1 Answers

You can try creating a virtual environment with python=3.8, if you are using anaconda then run the command conda create -n env1 python=3.8 After this to use the environment just run conda activate env1 and install only the required packages by your applications.

Advantage of doing this is it saves the time while compiling the EXE file and the file size will also be comparatively less.

I was facing the same issue, this trick worked for me.

You might not have to add the hooks to it also using this.

Remember to install pyinstaller in the environment and always activate before compiling the EXE.

like image 133
Sushant Agarwal Avatar answered Oct 11 '22 05:10

Sushant Agarwal