简体   繁体   中英

build python script to exe file using cx_freeze

i am trying to convert my python script to a exe file that anyone can run it from any computer, including computers with no python. so i see some guides that explain the best way is to use in the cx_freeze library. so i built a small gui application that use in tkinter only, this is my code:

import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()

and this is my setup file:

from cx_Freeze import setup, Executable
setup(
    name="GUI PROGRAM",
    version="0.1",
    description="MyEXE",
    executables=[Executable("try.py", base="Win32GUI")],
    )

and i run this command:

python setup.py build

and then i get this error:

KeyError: 'TCL_LIBRARY

and it only happens when i use tkinter. so i guess i miss something and i need to add in some way the tkinter to the setup file. can someone help me? thanks lot you guys.

Try you altering your setup script to this:

from cx_Freeze import setup, Executable
import os
import sys
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')

files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

setup(
    name="GUI PROGRAM",
    version="0.1",
    description="MyEXE",
    options = {"build_exe": files},
    executables=[Executable("try.py", base="Win32GUI")],
)

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') will remove the error message, while files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]} will include the missing Tk and Tcl runtimes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM