我试图从Python 3.5中使用cx_Freeze编写的脚本创build一个独立的exe,以便能够在没有Python的计算机上运行它。
程序本身只是一个简单的使用tkinter UI的小型计算器。 我使用Windows 10。
我创build了一个如下所示的安装脚本。
import sys from cx_Freeze import setup, Executable # replaces commandline arg 'build' sys.argv.append("build") filename = "Widgets_tmp.py" base = None if sys.platform == "win32": base = "Win32GUI" setup( name = "Widgets", version = "1.0", # options={"build_exe": {"packages": ["tkinter"]}}, description = "cx_Freeze Tkinter script", executables = [Executable(filename, base=base)])
这运行没有问题,直到我试图运行该exe文件。 然后我得到这个错误:
Traceback (most recent call last): File "C:\python64\lib\site-packages\cx_freeze\initscripts\Console.py" line 21, in <module> exec(code, m.__dict__) File "Widgets_tmp.py", line 1, in <module> File "C:\python64\lib\tkinter\__init__.py", line 35, in <module> import _tkinter#If this fails you Python may not be configured for Tk ImportError: DLL load failed:
我试着在代码中注释掉包含tkinter的代码,在“includes”中代替“packages”,但是结果相同。
也许我应该说Widgets_tmp.py中的第1行看起来像这样:
import tkinter as tk
我试图从源代码冻结示例代码,但得到相同的错误。 这两个代码工作完美使用Python。
我也试过使用:
options={"build_exe": {"includes": ["tkinter"]}},
但没有运气。
一个tkinter exe通过修改我的电脑上的setup.py正常运行。
OS = win7,Python = 3.5.2,cx_freeze = 5.0
setup.py:
includes = [] include_files = [r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll", \ r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"] setup( name = "Test", version = "1.0", options = {"build_exe": {"includes": includes, "include_files": include_files}}, executables = [Executable("test.py", base=base)] )
您必须修改(USERNAME)您的环境。