Python cx_freeze 错误

Posted

技术标签:

【中文标题】Python cx_freeze 错误【英文标题】:Python cx_freeze error 【发布时间】:2018-01-20 10:02:44 【问题描述】:

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'

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

【问题讨论】:

在您的程序中,您可能需要设置os.environ["TCL_LIBRARY"] 和库tcl 的完整路径(可以是fullpath/tcl.dllfullpath/libtcl.dll 【参考方案1】:

您需要包含 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"],

【讨论】:

以上是关于Python cx_freeze 错误的主要内容,如果未能解决你的问题,请参考以下文章

cx_Freeze:Python 错误主脚本。找不到模块错误:没有名为 pygments.lexers.python 的模块

Python cx_freeze 构建错误

Python 3.5.3 上的 cx_Freeze 错误

Python cx_freeze 错误

使用 cx_Freeze 冻结 Python 3.7 脚本时如何修复错误

cx_Freeze 错误模块 SSL 不可用 Python 3.7 Windows 10