6

I am using cx_freeze to transfer a python file to a exe. the problem is when i exclude tkinter in the setup.py, i can generate the exe file successfully, but when execute the exe file, it says No Module named tkinter.

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
                     "matplotlib","PIL"], "excludes": ["tkinter"]}

but when I try to include tkinter, it just can't generate the exe file.

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
                     "matplotlib","PIL","tkinter"]}
File "C:\Users\changchun_xu\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'
martineau
  • 119,623
  • 25
  • 170
  • 301
woniuwoniu
  • 87
  • 1
  • 8
  • are you sure it said `"No Module named tkinter".` and not `_tkinter`? Perhaps also check [this](http://stackoverflow.com/questions/35533803/keyerror-tcl-library-when-i-use-cx-freeze) question – Taku Mar 28 '17 at 02:23
  • thanks for your help! I look at the info again, the original info is "No module named 'tkinter'" – woniuwoniu Mar 28 '17 at 02:28
  • Maybe you can try following what [that](http://stackoverflow.com/questions/35533803/keyerror-tcl-library-when-i-use-cx-freeze/35706103) question recommended, by typing `os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python36-32\tcl\tcl8.6'` and `os.environ['TK_LIBRARY'] = r'C:\Program Files\Python36-32\tcl\tk8.6'` in the setup.py. **Note: replace the paths with your actual tcl and tk path.** – Taku Mar 28 '17 at 02:31
  • I did as you told me, I add the os.environ['TCL_LIBRARY'] = r'C:\python\tcl\tcl8.6' os.environ['TK_LIBRARY'] = r'C:\python\tcl\tk8.6' . And can add the tkinter to the packages: build_exe_options = {"packages": ["os","re","numpy","optparse","tkinter","time","linecache","pandas","matplotlib","optparse","PIL","glob"]}. This time i can generate the exe, but when execute the exe file ,it says "import _tkinter # If this fails your Python may not be configured for TK", you mentioned this in your first comment. I reinstall the python, still have the same problem – woniuwoniu Mar 28 '17 at 07:06
  • @abccd, I wonder how do you know I may come across some problem like "_tkiner"? – woniuwoniu Mar 28 '17 at 07:32
  • It's a very common tkinter compile error, add the two include_files stated by MrLeeh will solve it. – Taku Mar 28 '17 at 14:40
  • Thanks for your help! For my problem it seems the matplotlib calls the "TK", actually I just add the matplotlib.use("Agg") in my python file will solve the problem. The next time if I have to use the "TK", I will try your suggestion. – woniuwoniu Mar 29 '17 at 01:26

1 Answers1

10

You have to make two modifications to your setup.py to get things working:

  1. Set TCL-LIBRARY and TK_LIBRARY environment variables. (You already did this)

  2. Add the tcl86t.dll and tk86t.dll to your include_files parameter

So the setup.py should look something like this:

import os
from cx_Freeze import setup, Executable

os.environ['TCL_LIBRARY'] = 'c:/python36/tcl/tcl8.6'
os.environ['TK_LIBRARY'] = 'c:/python36/tcl/tk8.6'

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(
    packages = [],
    excludes = [],
    include_files=['c:/python36/DLLs/tcl86t.dll', 'c:/python36/DLLs/tk86t.dll']
)

import sys
base = 'Win32GUI' if sys.platform=='win32' else None

executables = [
    Executable('editor.py', base=base)
]

setup(name='editor',
      version = '1.0',
      description = '',
      options = dict(build_exe = buildOptions),
      executables = executables)
MrLeeh
  • 5,321
  • 6
  • 33
  • 51
  • Thanks for your help! For my problem it seems the matplotlib calls the "TK", actually I just add the matplotlib.use("Agg") in my python file will solve the problem. The next time if I have to use the "TK", I will try your suggestion. – woniuwoniu Mar 29 '17 at 01:26