Search code examples
pythontkintercx-freeze

CX_freeze executable will not work. _tkinter DLL load failed


I have the same problem as many other questions, but found no solution working yet. This is on Windows 10 64 bit, Python 3.6 32 bit.

I have tried uninstalling several times, 64 bit Python, various combinations of paths and variables in the setup file.

I do find it confusing that the traceback from the exe file refers to filepaths in my Python folder, not the build folder where the executable resides. I would have thought that this exe should now be "innocent" of the existence of the python folder?

Output from exe file

    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.

My setup file for 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")])

Solution

  • I found the answer at last. The lines in the setup file to include the files tcl86t.dll and tk86t.dll weren't doing their job for some reason. Must be some error in the path formatting.

    What worked was copying them manually from the Python\Dlls folder and pasting them into the exe.win folder where the new executable resides.

    I subsequently discovered here a setup.py script that gets the correct paths. V happy now.

    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")])