我有一个 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,并且在我的主 Python 脚本中使用了“import tkinter”。主 python fyle 使用命令和 python mainprog.py 正常执行,但问题出在通过 build 命令创建时的 .exe 文件中。
最佳答案
排除表示不包含该包。我建议您从安装脚本中的 excepts = ['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)]
)
关于python - 没有名为 tkinter 的模块,当使用 cx-freeze 时,即使我指定了模块的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45096619/