我最近编写了一个库(在 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 *
和 profiler.py
在tensorflow.python.profiler
然后尝试执行导致错误的导入。
我找到了导致错误的文件,当我在 IDLE 上找到时 from tensorflow.core.profiler.tfprof_log_pb2 import OpLogProto
,它工作得很好。
在到达那个点之前,我遇到了几个类似的问题(cx_Freeze 构建没有显示任何警告或错误,但是 .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,因此此解决方案可能特定于我的安装。
关于python - cx_Freeze 没有找到一些 TensorFlow 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123840/