Skip to main content
 首页 » 编程设计

python之没有名为 tkinter 的模块,使用 cx-freeze 时,即使我指定了模块的路径

2024年10月01日9Renyi-Fan

我有一个 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文件使用comand python mainprog.py正常执行,但问题出在build命令创建的.exe文件中。

请您参考如下方法:

Excludes 表示不包含包。我建议您从安装脚本中的 excludes = ['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)] 
 )