cx_Freeze 未找到一些 TensorFlow 导入

cx_Freeze not finding some TensorFlow imports

我最近编写了一个库(在 Python 3.6 中)并在 Windows 10 上使用 tkinter 为它构建了一个 GUI。GUI 现在已经完成,我正在尝试冻结它使用cx_Freeze。

安装脚本 运行 非常好(或者至少我没有发现任何错误消息或警告),我可以从中获取我的可执行文件。问题是,当我 运行 它时,我收到以下错误消息:

File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\profiler\profiler.py", line 22 in <module>
from tensorflow.core.profiler.tfprof_log_pb2 import OpLogProto
ModuleNotFoundError: No module named 'tensorflow.core.profiler.tfprof_log_pb2'

这里之所以提到TensorFlow,是因为我的库用的是TensorFlow,当然我的GUI也是用的。整个错误消息说的是,当我导入 tensorflow (import tensorflow as tf) 时,程序尝试执行 from tensorflow.python import *tensorflow.python.profiler 中的 profiler.py 然后尝试执行导致错误的导入。

我找到了导致错误的文件,当我在 IDLE from tensorflow.core.profiler.tfprof_log_pb2 import OpLogProto 上这样做时,它工作得很好。

在到达那个点之前,我遇到了几个类似的问题(cx_Freeze building 没有显示任何警告或错误,但是 .exe 有一些 import 错误),但到目前为止我可以我自己修复它们,主要是将它们添加到安装脚本中的 include_files 列表中。我尝试对这个 TensorFlow 文件执行相同的操作,但没有成功。我还尝试将 TensorFlow 作为一个包包含在安装脚本中,或者直接将其全部导入我的 main.py,但没有成功。

我的setup.py如下(可能有一些不必要的包含,因为我尝试了很多东西):

from cx_Freeze import setup, Executable
import os
import sys

os.environ['TCL_LIBRARY'] = "C:\Program Files\Python36\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\Program Files\Python36\tcl\tk8.6"

base = None
if sys.platform == "win32":
    base = "Win32GUI"

includes = ["tkinter", "_tkinter", "numpy.core._methods", "numpy.lib.format", "tensorflow"]
include_files = ["C:\Program Files\Python36\DLLs\tcl86t.dll",
                 "C:\Program Files\Python36\DLLs\tk86t.dll",
                 "C:\Program Files\Python36\DLLs\_tkinter.pyd",
                 "C:\Program Files\Python36\Lib\site-packages\tensorflow\core\profiler\tfprof_log_pb2.py",
                 "C:\Program Files\Python36\Lib\site-packages\tensorflow\python\profiler\profiler.py",
                 "C:\Program Files\Python36\Lib\site-packages\tensorflow\include\tensorflow\core\profiler\tfprof_log.pb.h"]
packages = []

setup(name = "Ap'Pear",
      version = "0.1",
      description = "Test executable",
      options = {"build_exe": { "includes": includes, "include_files": include_files, "packages": packages}},
      executables = [Executable(script = "main.py", targetName = "Ap'Pear.exe", base = base, icon = "images/icon.ico")],
      )

我尝试从头开始重建 TensorFlow 及其依赖项,但也没有解决任何问题。

提前致谢!

我可以通过在 \path\to\python\Lib\site-packages\tensorflow\core\profiler 中创建空白 __init__.py 文件来解决这个问题。我是 运行 python 3.5.2 和 TensorFlow 1.5.0,所以这个解决方案可能特定于我的安装。