我有一个python脚本,我试图使用cx-freeze使其可执行。这是我的script.py文件
from cx_Freeze import setup,Executable
import tkinter
import sys
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\Admin\\Anaconda3\\tcl\\tcl8.6"
os.environ['TCL_LIBRARY'] = "C:\\Users\\Admin\\Anaconda3\\tcl\\tk8.6"
includes = []
excludes = ['tkinter']
packages = []
base = "Win32GUI"
setup(
name = 'myapp',version = '0.1',description = 'app',author = 'user',
options = {'build_exe': {'excludes':excludes,'packages':packages}},
executables = [Executable('emetor1.py')]
)
使用" python script.py build"执行时,使用.exe文件创建构建文件夹。但是当我执行.exe文件时,它给了我" ModuleNotFoundError:没有名为tkinter"的模块。我把os.environ放在包的路径上,但我仍然不明白为什么它不能识别它。如果有人知道如何解决这个问题,我会非常感激。
我正在使用Windows,我使用" import tkinter"在我的主要python脚本中。主python fyle通常使用comand python mainprog.py执行,但问题是在build命令创建的.exe文件中。
答案 0 :(得分:2)
排除意味着不包含该包。我建议你删除' tkinter'来自设置脚本中的排除= [' tkinter']。
编辑:尝试使用此安装脚本:
from cx_Freeze import setup,Executable
import sys
import os
os.environ['TCL_LIBRARY'] = r'C:\Users\Admin\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Admin\Anaconda3\tcl\tk8.6'
includes = []
include_files = [r"C:\Users\Admin\Anaconda3\DLLs\tcl86t.dll",
r"C:\Users\Admin\Anaconda3\DLLs\tk86t.dll"]
packages = []
base = "Win32GUI"
setup(
name = 'myapp',version = '0.1',description = 'app',author = 'user',
options = {'build_exe': {'includes':includes, 'include-files':include_files,'packages':packages}},
executables = [Executable('emetor1.py', base=base)]
)
答案 1 :(得分:0)
经过大量研究,并按照说明进行操作,但没有任何效果。我检查了我在lib中是否已经有tcl8.6和tk8.6文件,但是我注意到Tkinter文件夹以大写字母开头,并且它查找“ tkinter”,因此我将“ T”更改为“ t”,令人惊讶的是,它似乎起作用。
答案 2 :(得分:-1)
from cx_Freeze import setup, Executable
import tkinter
import sys
import os
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "includes": ["tkinter"],"include_files": ["ico.png"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name="merge",
version="0.1",
description="My GUI application!",
options={"build_exe": build_exe_options},
executables=[Executable("merge.py", base=base)])
有类似的问题。 setup.py 的这种格式已为我解决。