问题
i am trying to build an exe file from my python script using the cx_freeze library. this is my code:
import easygui
easygui.ynbox('Shall I continue?', 'Title', ('Yes', 'No'))
and this is my setup code:
import cx_Freeze
import sys
import matplotlib
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [cx_Freeze.Executable("tkinterVid28.py", base=base, icon="clienticon.ico")]
cx_Freeze.setup(
name = "SeaofBTC-Client",
options = {"build_exe": {"packages":["easygui","matplotlib"]}},
version = "0.01",
description = "Sea of BTC trading application",
executables = executables
)
and then i get this error:
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
self._LoadModule(name, fp, path, info, deferredImports, parent)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
self._RunHook("load", module.name, module)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
method(self, *args)
File "C:\Python36\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
tclSourceDir = os.environ["TCL_LIBRARY"]
File "C:\Python36\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'
回答1:
Easygui uses some Tkinter so when compiling it you must include the Tkinter libraries.
this should just be a question of using the include_files
arguement
Add the following arguments to your script:
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"], , "clienticon.ico" ], "packages": ["easygui","matplotlib"]}
next alter:
options = {"build_exe": {"packages":["easygui","matplotlib"]}},
to:
options = {"build_exe": files},
and everything should work. Your script should now look like this:
import cx_Freeze
import sys
import matplotlib
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", "easygui","matplotlib"]}
base = None
if sys.platform == 'win32':
base = "Win32GUI"
executables = [cx_Freeze.Executable("tkinterVid28.py", base=base,
icon="clienticon.ico")]
cx_Freeze.setup(
name = "SeaofBTC-Client",
options = {"build_exe": files},
version = "0.01",
description = "Sea of BTC trading application",
executables = executables
)
There is another error in your script as well. Because you did not use include_files
argument to include the icon you want to use. It would not appear in the executable icon or in the output (if you used it in your tkinterVid28.py file) this would generate an error.
Oh and unless you have a reason to do so I cannot see why you imported matplotlib. Cx_Freeze detects imports in the script you are trying to convert to an executable and not in the setup script itself but it is always a good idea to list them in packages.
I hope this sorted your problem
来源:https://stackoverflow.com/questions/45401552/tcl-library-in-cx-freeze