Python PyInstaller 4.0 打包 TensorFlow 2.0 项目无法正常工作 ImportError : cannot import name 'pywrap_tensorflow'

标签 python tensorflow keras pyinstaller

我正在尝试使用使用tensorflow的pyinstaller捆绑一个python应用程序。

我现在在虚拟环境中使用 Pyinstaller-4.0.dev0+2f4426f52、Tensorflow 2.0、Keras 2.3 和 Python 3.7.3。

我尝试过各种旧版本,但每个旧版本都有一个我无法解决的不同问题。我花了 3 天的时间来尝试解决这个问题。

我正在运行的脚本的简化示例是:

from tensorflow import keras
model = keras.Sequential()

我使用以下命令来运行 pyinstaller:

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

使用隐藏导入似乎是多余的,正如输出所示:

DEBUG: Hidden import 'tensorflow_core' already found

日志中没有构建错误或警告。

我已查看 pyz-00.pyz 文件,并可以确认该文件中包含 TensorFlow 模块。我确认存档包含:

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

但是,当我尝试运行 exe 时,出现以下错误:

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)

看来 pyinstaller 无法处理 tensorflow ,我想知道这里有经验的人是否知道如何为其编写一个钩子(Hook)或知道其他一些解决方法?

最佳答案

您可以尝试更改调用该程序的目录吗?有时,当当前目录中有一个带有 init.py 的 tensorflow 子目录时,会出现类似的错误,Python 会优先选择该子目录而不是任何其他安装。您还可以引用以下链接了解更多故障排除步骤:

Tensorflow-gpu with pyinstaller

关于Python PyInstaller 4.0 打包 TensorFlow 2.0 项目无法正常工作 ImportError : cannot import name 'pywrap_tensorflow' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237216/

相关文章:

python - 为什么我的 keras LSTM 模型会陷入无限循环?

python - 如何通过 SOAPPy (python) 或 SUDS 发送嵌套的 SOAP 请求

python - 如何在 pandas DataFrame 中按列设置 dtypes

Tensorflow 对象检测 API 无效参数 : Shape mismatch in tuple component 16. 预期 [1,?,?,3],得到 [1,182,322,4]

python - 按键分组 tensorflow 数据集,按键分组

python - 连接两层

python - 模块未找到错误 : No module named 'pynput'

python - 使用 BeautifulSoup 解析 HTML

tensorflow - 在 ML Engine 中运行分布式训练的正确方法

python - Keras:处理线程和大型数据集