问题:使用 cx_Freeze 和 tkinter 时,我得到:“DLL 加载失败:找不到指定的模块。” (Python 3.5.3)

使用 cx_Freeze 和 Tkinter 时,我收到以下消息:

File "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.

需要注意的一些事项:

  • 我想使用 Python 3+(目前使用 3.5.3,32 位)。不要真正关心一个特定的版本,无论什么工作。

  • 我的项目有多个文件需要编译。据我所知,这给我留下了 cx_Freeze 或 Nuitka。 Nuitka 有自己的问题。

  • 我使用的是 Windows 10 家庭版,64 位

这是我当前的 setup.py:

from cx_Freeze import setup, Executable    
import sys  

build_exe_options = {"packages": ["files", "tools"]}  

base = None    
if sys.platform == "win32":    
    base = "Win32GUI"    

setup(name="Name",  
      version="1.0",  
      description="Description",  
      options={"build_exe": build_exe_options},  
      executables=[Executable("main.py", base=base)],  
      package_dir={'': ''},  
      )

我尝试了来自互联网各个角落的许多解决方案。包括但不仅限于:

  • python的多个版本(以及对应的cx_Freeze/Tkinter版本)

  • 32位和64位版本

  • 用easygui替换Tkinter(显然easygui需要Tkinter才能工作)

  • 检查 PATH 变量

  • 重新启动我的电脑(不知道我的预期)

  • 卸载其他版本的python并修复正确的版本

  • 将以下内容放在我的编译 bat 文件中(绝对是正确的路径):

设置 TCL_LIBRARYu003dC:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32 cl cl8.6
设置 TK_LIBRARYu003dC:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32 cl k8.6
  • 在我的 setup.py 中放置以下内容:
    options={"build_exe": {"includes": ["tkinter"]}}
  • 连同:
    include_files = [r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll",\
                     r"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"]

(是的,那些以一种或另一种方式包含在 setup() 中)


感谢您的帮助,非常感谢。是的,我已经在这个网站上查看了这个问题的几乎所有解决方案。希望有人可以帮助我找到另一种解决方案,因为我的问题似乎一直存在。

解答

找到了解决方案!

我必须将 tk86t.dll 和 tcl86t.dll 文件从我的 python 目录的 DLLs 文件夹复制到构建文件夹中,其中包含我试图编译的 main.py。

这与拥有

set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tcl8.6  
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35\tcl\tk8.6

在我的 compile.bat 的顶部,包括

"include_files": ["tcl86t.dll", "tk86t.dll"]

在我的 setup.py 中的 build_exe_options 中,似乎已经成功了。

这是我当前的 setup.py:

from cx_Freeze import setup, Executable  
import sys  

build_exe_options = {"packages": ["files", "tools"], "include_files": ["tcl86t.dll", "tk86t.dll"]}  

base = None  
if sys.platform == "win32":  
    base = "Win32GUI"  

setup(name="Name",  
    version="1.0",  
    description="Description",  
    options={"build_exe": build_exe_options},  
    executables=[Executable("main.py", base=base)],  
    package_dir={'': ''},  
    )  

这是我的 compile.bat(已更新以显示所有步骤):

@echo off
set TCL_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6
set TK_LIBRARY=C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6
RD /S /Q "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
mkdir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tcl86t.dll"
xcopy /s "C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll" "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin\tk86t.dll"
cd "C:\Users\VergilTheHuragok\Desktop\PythonProject\"
cxfreeze main.py --target-dir "C:\Users\VergilTheHuragok\Desktop\PythonProjectCompiled\bin" --target-name "launch.exe"
pause  

我在这里找到了这个解决方案。

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐