Moiz Sohail
Moiz Sohail

Reputation: 648

Error during packaging tflite runtime with pysintaller

I am trying to package the tflite runtime in pyinstaller so that I can share the file with others. However everytime I run the packaged program. It gives me the following error:

ModuleNotFoundError: No module named 'tensorflow'
[15468] Failed to execute script gallerycleaner

It works fine when it's in .py format. I even tried adding the tflite runtime in the hidden imports section but it still doesn't work

hiddenimports=['tflite_runtime'],

Please note I cannot import the whole tensorflow package because that way the file size increases drastically.

Upvotes: 0

Views: 596

Answers (1)

Gabriel Faria
Gabriel Faria

Reputation: 50

If we peek into the interpreter module from the tflite_runtime package, we'll find the following code.

# interpreter.py
if not __file__.endswith(os.path.join('tflite_runtime', 'interpreter.py')):
  # This file is part of tensorflow package.
  from tensorflow.python.util.lazy_loader import LazyLoader
  from tensorflow.python.util.tf_export import tf_export as _tf_export

It appears that pyinstaller messes up the __file__ variable during the bundling process. As a result, this code branch (which wouldn't be executed otherwise) is run, causing the tensorflow import attempt. You can read more about this behavior here.

One possible approach, though not ideal, is to replace the if statement with the following code before bundling the application.

# interpreter.py
if False:

That way, the application won't try to import tensorflow and the issue should be solved.

Upvotes: 3

Related Questions