Search code examples
pythondllbuildexecx-freeze

Cx_Freeze building without error but exe doesn't open


I am attempting to build an exe using cx_Freeze which uses several modules:

import tkinter as tk
from tkinter import ttk
import random, time, bluetooth, json, sys, os
from _thread import *
from threading import Thread, Lock

When I attempt to build the exe, it seems to work perfectly: it raises no errors and creates the build folder containing the exe file. However, when I attempt to open the exe file, it simply doesn't open. If briefly seems to flash a windows but then disappears. My setup.py is this:

from cx_Freeze import setup,Executable
import sys
import os

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')

includes = []
include_files = []
packages = []
base = "Win32GUI"
setup(
    name = 'Buzzer',
    version = '0.1',
    description = 'Buzzer application',
    author = 'Me',
    executables = [Executable('Buzzer.py')]
)

The flashing screen contains the following traceback:

Traceback (most recent call last): File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run module.run() File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run exec(code, m.dict) File "print.py", line 1, in File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 36, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: DLL load failed: The specified module could not be found.


Solution

  • You need to tell cx_Freeze to include the TCL and TK DLLs in the build directory.

    For cx_Freeze 5.1.1 (the current version) or 5.1.0, the DLLs need to be included in a lib subdirectory of the build directory. You can do that by passing a tuple (source, destination) to the corresponding entry of the include_files list option:

    include_files = [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
                     (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))]
    

    You need to pass this include_files list to the build_exe options in the setup call (and you should also pass the baseyou have defined to the Executable):

    setup(
        name = 'Buzzer',
        version = '0.1',
        description = 'Buzzer application',
        author = 'Me',
        options={'build_exe': {'include_files': include_files}},
        executables = [Executable('Buzzer.py', base=base)]
    )
    

    For other cx_Freeze versions, the DLLs need to be included directly in the build directory. This can be done with:

    include_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
                     os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]