使用 pysintaller 打包 tflite 运行时出错

Error during packaging tflite runtime with pysintaller

我正在尝试将 tflite 运行time 打包到 pyinstaller 中,以便我可以与其他人共享该文件。但是每次我运行打包程序。它给了我以下错误:

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

当它是.py 格式时它工作正常。我什至尝试在隐藏的导入部分添加 tflite 运行time,但它仍然不起作用

hiddenimports=['tflite_runtime'],

请注意我无法导入整个 tensorflow 包,因为这样文件大小会急剧增加。

如果我们查看 tflite_runtime 包中的 interpreter 模块,我们将看到以下代码。

# 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

似乎 pyinstaller 在捆绑过程中与 __file__ 混淆了。结果,这个不会以其他方式执行的代码分支是 运行,导致 tensorflow 导入。您可以阅读更多相关信息 here

虽然不理想,但我采用的方法是在捆绑应用程序之前将 if 语句替换为该语句。

# interpreter.py
if False:

这样,应用程序就不会尝试导入 tensorflow,您应该解决问题。