Search code examples
python-3.xcx-freeze

KeyError: 'TCL_LIBRARY' when building using cx_freeze


When trying to build my file using cx_freeze it spits out an error

    raise KeyError(key) from None
     KeyError: 'TCL_LIBRARY'

I know there are other posts describing this, but I already tried adding

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll')
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')

which is what other posts recommend, but i still get the same error

This is my code so far

from cx_Freeze import setup, Executable
import sys, os


shortcut_table = [
    ("DesktopShortcut",        # Shortcut
     "DesktopFolder",          # Directory_
     "program",           # Name
     "TARGETDIR",              # Component_
     "[TARGETDIR]main.exe",# Target
     None,                     # Arguments
     None,                     # Description
     None,                     # Hotkey
     "icon.ico",                     # Icon
     None,                     # IconIndex
     None,                     # ShowCmd
     'TARGETDIR'               # WkDir
     )
    ]

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

os.environ['TCL_LIBRARY'] = "C:\\Program Files\\Python35-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files\\Python35-32\\tcl\\tk8.6"


msi_data = {"Shortcut": shortcut_table}
bdist_msi_options = {'data': msi_data}

executables = [Executable("main.py", shortcutName='2048', shortcutDir='DesktopFolder', icon='icon.ico', base=base), Executable("extras.pyw"),]


setup(
    name = '2048',
    author = 'Ethan',
    options={
        "build_exe": {
            "packages":["pygame", "sys", "random", "os", "ctypes"],
            "include_files":["scores.txt",
            "icon.ico",
            ]
            }},
    executables = executables,
    version = "1.0"
)

edit: changes to code

after changing the code, it currently runs without error but only creates a folder with an ul-launchable exe

build
   exe.win-amd64-3.6
      api-ms-win-crt-conio-l1-1-0.dll
      api-ms-win-crt-convert-l1-1-0.dll
      api-ms-win-crt-environment-l1-1-0.dll
      api-ms-win-crt-filesystem-l1-1-0.dll
      api-ms-win-crt-heap-l1-1-0.dll
      api-ms-win-crt-locale-l1-1-0.dll
      api-ms-win-crt-math-l1-1-0.dll
      api-ms-win-crt-process-l1-1-0.dll
      api-ms-win-crt-runtime-l1-1-0.dll
      api-ms-win-crt-stdio-l1-1-0.dll
      api-ms-win-crt-string-l1-1-0.dll
      api-ms-win-crt-time-l1-1-0.dll
      main.exe
      python36.dll
      VCRUNTIME140.dll

EDIT 2: so i did some more tinkering and found out the TCL error comes from including ctypes(not sure why). So i removed it and changed my python version to 3.6 and it built perfectly. But i would prefer to have ctypes included as i use it to disable application scaling.

is there another way to deal with screen scaling or a way to fix the TCL error?


Solution

  • cx_freeze does not work with python 3.7 so switching to 3.6 allowed it to work, then adding

    import os.path
    
    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')
    

    sets the directory in order to use ctypes