Python cx_freeze错误

时间:2018-01-20 10:02:44

标签: python tkinter pygame cx-freeze

Python版本:3.6 最新版本的包

您好,我的设置需要帮助, 我有设置文件,但它不起作用,我不知道为什么

import cx_Freeze
import sys
import matplotlib

base = None

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

executables = [cx_Freeze.Executable("python.py", base=base,)]

cx_Freeze.setup(
    name = "music-Client",
    options = {"build_exe": {"packages":["tkinter","matplotlib","Pygame"]}},
    version = "1",
    description = "hello",
    executables = executables
    )

当我尝试用cmd构建它时,这个很长的错误来了

Traceback (most recent call last):
  File "setup.py", line 17, in <module>
    executables = executables
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "E:\Python\Lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "E:\Python\Lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "E:\Python\Lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "E:\Python\Lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "E:\Python\Lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "E:\Python\Lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\dist.py", line 219, in run
    freezer.Freeze()
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\freezer.py", line 616, in Freeze
    self.finder = self._GetModuleFinder()
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\freezer.py", line 342, in _GetModuleFinder
    finder.IncludePackage(name)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 659, in IncludePackage
    module = self._ImportModule(name, deferredImports)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 311, in _ImportModule
    deferredImports, namespace = namespace)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 404, in _InternalImportModule
    parentModule, namespace)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 417, in _LoadModule
    namespace)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 486, in _LoadPackage
    self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 464, in _LoadModule
    self._RunHook("load", module.name, module)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\finder.py", line 537, in _RunHook
    method(self, *args)
  File "E:\Projekty\venv\lib\site-packages\cx_Freeze\hooks.py", line 615, in load_tkinter
    tclSourceDir = os.environ["TCL_LIBRARY"]
  File "E:\Projekty\venv\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'

我不知道如何解决这个问题,任何人都可以帮助我吗? 我真的需要帮助,谢谢你的答案

1 个答案:

答案 0 :(得分:2)

您需要包含tkinter库。使用os.environ()方法可以轻松完成此操作。

它应该是这样的:

import os
os.environ["TCL_LIBRARY"] = "<PathToPython>/Python36-32/tcl/tcl8.6"
os.environ["TK_LIBRARY"] = "<PathToPython>/Python36-32/tcl/tk8.6"

在你的脚本中它可能看起来像这样:

import cx_Freeze
import sys
import os
os.environ["TCL_LIBRARY"] = "<PathToPython>/Python36-32/tcl/tcl8.6"
os.environ["TK_LIBRARY"] = "<PathToPython>/Python36-32/tcl/tk8.6"

base = None

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

executables = [cx_Freeze.Executable("python.py", base=base,)]

cx_Freeze.setup(
    name = "music-Client",
    options = {"build_exe": {"packages":["tkinter","matplotlib","Pygame"]}},
    version = "1",
    description = "hello",
    executables = executables
    )

请注意,我删除了import mathplotlib,因为在安装脚本中不需要这样做,通常会检测到依赖关系。

如果这还不够:

通常会返回另一个错误。这是缺少的运行时错误。您可以使用include_files方法包含运行时(如文档中所述)。我们可以在此行中包含此声明:

options = {"build_exe": {"packages":["tkinter","matplotlib","Pygame"]}},

像这样:

options = {"build_exe": {"packages":["tkinter","matplotlib","Pygame"], "include_files":["<PathToPython>/Python36-32/DLLs/tk86t.dll", "<PathToPython>/Python36-32/DLLs/tcl86t.dll"]}},