python 3.6中的cx_freeze
Posted
技术标签:
【中文标题】python 3.6中的cx_freeze【英文标题】:cx_freeze in python 3.6 【发布时间】:2017-09-25 05:28:06 【问题描述】:我在 python 3.6 中编写了一个代码,并想为该代码制作一个 exe 文件。我为此使用了 cx_freeze。代码没有错,但我认为问题出在 setup.py 文件中。这是我的代码:
from tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()
我的 cx_freeze setup.py 文件是这样的:
# setup file for cx_freeze
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
includes = ["atexit", "re"]
build_exe_options = "packages": ["os", "tkinter"], "includes": includes
setup(
name = "testing",
version = "1.0",
description = "Testing of tkinter",
options = "build_exe": build_exe_options,
executables = [Executable("testing.py", base = base)]
)
当我将脚本转换为 exe 文件时,出现以下错误。谁能告诉我 setup.py 文件有什么问题?
running build
running build_exe
Traceback (most recent call last):
File "setup.py", line 20, in <module>
executables = [Executable("tk1.py", base = base)]
File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
distutils.core.setup(**attrs)
File "C:\Python36\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python36\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python36\lib\distutils\command\build.py", line 135, in run
self.run_command(cmd_name)
File "C:\Python36\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 219, in run
freezer.Freeze()
File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
self.finder = self._GetModuleFinder()
File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 340, in _GetModuleFinder
finder.IncludePackage(name)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 653, in IncludePackage
module = self._ImportModule(name, deferredImports)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 310, in _ImportModule
deferredImports, namespace = namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 403, in _InternalImportModule
parentModule, namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 416, in _LoadModule
namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
self._LoadModule(name, fp, path, info, deferredImports, parent)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
self._RunHook("load", module.name, module)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
method(self, *args)
File "C:\Python36\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
tclSourceDir = os.environ["TCL_LIBRARY"]
File "C:\Python36\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'
当我在 setup.py 文件中添加以下两行时,它会将我的脚本转换为 exe。
os.environ['TCL_LIBRARY'] = r'C:\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Python36\tcl\tk8.6'
但是当我运行exe文件时,我得到了如下图所示的错误。
【问题讨论】:
这不可能是 Python 3.6 代码,因为Tkinter
在 Python 3 中被重命名为 tkinter
。
抱歉错误,请再次检查错误。 @skrx
How to include tkinter when using cx_freeze to convert script to .exe?的可能重复
How to include tkinter when using cx_freeze to convert script to .exe?的可能重复
您是否已将"include_files": ['c:/python36/DLLs/tcl86t.dll', 'c:/python36/DLLs/tk86t.dll']
添加到您的build_exe_options
字典中?
【参考方案1】:
您拥有 tcl/tk 库文件夹,但您只是缺少运行时或 DLL。
您还需要使用include_files
参数来获取运行时。
如果您给出的路径是正确的,那么它们都应该位于C:/python36/DLLs/
。您需要tcl86t.dll
和tk86t.dll
,所以只需将它们包含在您的安装脚本中即可。这很简单:
build_exe_options = "packages": ["os", "tkinter"], "includes": includes, "include_files": ["C:/python36/DLLs/tcl86t.dll", "C:/python36/DLLs/tk86t.dll"]
This answer also relates to your problem.
希望我对您有所帮助。对不起,我才发现这个问题。
【讨论】:
以上是关于python 3.6中的cx_freeze的主要内容,如果未能解决你的问题,请参考以下文章
cx_Freeze ModuleNotFoundError: 没有名为“编解码器”的模块
Windows XP 中的 Python Cx_Freeze 错误
cx_Freeze:主脚本中的 Python 错误 - ModuleNotFoundError:没有名为 'scipy.spatial.ckdtree 的模块