CX_freeze 可执行文件将无法运行。 _tkinter DLL 加载失败

CX_freeze executable will not work. _tkinter DLL load failed

我遇到了与许多其他问题相同的问题,但尚未找到有效的解决方案。这是 Windows 10 64 位,Python 3.6 32 位。

我试过多次卸载,64 位 Python,安装文件中路径和变量的各种组合。

我发现 exe 文件的回溯指的是我的 Python 文件夹中的文件路径,而不是可执行文件所在的构建文件夹,这让我感到困惑。我还以为这个exe现在应该是python文件夹存在的"innocent"?

从exe文件输出

    Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "main3.py", line 2, in <module>
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\appJar\__init__.py", line 2, in <module>
    from appJar.appjar import gui
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\appJar\appjar.py", line 23, in <module>
    from tkinter import *
  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 36, 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.

我的 cx_freeze 安装文件 -

from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'C:\Program Files (x86)\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files (x86)\Python36-32\tcl\tk8.6'

build_exe_options={
"includes": [],
"packages": ["os","tkinter"],
"include_files" : [r'C:\Program Files (x86)\Python36-32\DLLs\tcl86t.dll',
r'C:\Program Files (x86)\Python36-32\DLLs\tk86t.dll']
}

setup(name = "main" ,
      version = "0.1" ,
      description = "" ,
      options={"build.exe":build_exe_options},
      executables = [Executable("main3.py", base=None, targetName="hexml.exe")])

我终于找到了答案。安装文件中包含文件 tcl86t.dlltk86t.dll 的行由于某种原因没有完成它们的工作。一定是路径格式有误。

有效的方法是从 Python\Dlls 文件夹中手动复制它们并将它们粘贴到新可执行文件所在的 exe.win 文件夹中。

我随后发现 here 一个获取正确路径的 setup.py 脚本。 V开心了

from cx_Freeze import setup, Executable
import os
import sys

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

options = {
    'build_exe': {
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
         ],
    },
}

setup(name = "main" ,
      version = "0.1" ,
      description = "" ,
      options=options,
      executables = [Executable("main3.py", base=None, targetName="hexml.exe")])