Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError: 'TCL_Library' when I use cx_Freeze

When I use cx_Freeze I get a keyerror KeyError: 'TCL_Library'while building my pygame program. Why do I get this and how do I fix it?

My setup.py is below:

from cx_Freeze import setup, Executable

setup(
    name = "Snakes and Ladders",
    version = "0.9",
    author = "Adam",
    author_email = "Omitted",
    options = {"build_exe": {"packages":["pygame"],
                         "include_files": ["main.py", "squares.py",
                         "pictures/Base Dice.png", "pictures/Dice 1.png",
                         "pictures/Dice 2.png", "pictures/Dice 3.png",
                         "pictures/Dice 4.png", "pictures/Dice 5.png",
                         "pictures/Dice 6.png"]}},
    executables = [Executable("run.py")],
    )
like image 530
Orange1861 Avatar asked Feb 21 '16 08:02

Orange1861


3 Answers

You can work around this error by setting the environment variables manually:

set TCL_LIBRARY=C:\Program Files\Python35-32\tcl\tcl8.6
set TK_LIBRARY=C:\Program Files\Python35-32\tcl\tk8.6

You can also do that in the setup.py script:

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

setup([..])

But I found that actually running the program doesn't work. On the cx_freeze mailinglist it was mentioned:

I have looked into it already and no, it is not just a simple recompile -- or it would have been done already! :-)

It is in progress and it looks like it will take a bit of effort. Some of the code in place to handle things like extension modules inside packages is falling over -- and that may be better solved by dropping that code and forcing the package outside the zip file (another pull request that needs to be absorbed). I should have some time next week and the week following to look into this further. So all things working out well I should put out a new version of cx_Freeze before the end of the year.

But perhaps you have more luck ... Here's the bug report.

like image 91
Martin Tournoij Avatar answered Nov 04 '22 21:11

Martin Tournoij


Instead of setting the environment variables using installation specific absolute paths like C:\\LOCAL_TO_PYTHON\\... you may also derive the necessary paths dynamically using the __file__ attribute of Python standard package like os:

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

After this fix the executable file will be created, but you will probably get a "DLL not found error" when you try to execute it - at least with Python 3.5.3 and cx_Freeze 5.0.1 on Windows 10.

When you add the following options, the necessary DLL-files will be copied automatically from the Python-Installation directory to the build-output of cx-Freeze and you should be able to run your Tcl/Tk application:

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

# ...

setup(options = options,
      # ...
)
like image 36
D. L. Müller Avatar answered Nov 04 '22 21:11

D. L. Müller


Just put this before the setup at setup.py

import os

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

And run it:

python setup.py bdist_msi

This worked fine for me.

like image 15
Guilherme Avatar answered Nov 04 '22 20:11

Guilherme