Python KeyError: 'TCL_Library' 当我使用 cx_Freeze 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35533803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:34:06  来源:igfitidea点击:

KeyError: 'TCL_Library' when I use cx_Freeze

pythonpython-3.xcx-freeze

提问by Orange1861

When I use cx_FreezeI get a keyerror KeyError: 'TCL_Library'while building my pygame program. Why do I get this and how do I fix it?

当我使用时,cx_FreezeKeyError: 'TCL_Library'在构建我的 pygame 程序时遇到一个关键错误。为什么我会得到这个,我该如何解决?

My setup.py is below:

我的 setup.py 如下:

from cx_Freeze import setup, Executable

setup(
    name = "Snakes and Ladders",
    version = "0.9",
    author = "Adam",
    author_email = "Omitted",
    options = {"build_exe": {"packages":["pygame"],
                         "include_files": ["main.py", "squares.py",
                         "pictures/Base Dice.png", "pictures/Dice 1.png",
                         "pictures/Dice 2.png", "pictures/Dice 3.png",
                         "pictures/Dice 4.png", "pictures/Dice 5.png",
                         "pictures/Dice 6.png"]}},
    executables = [Executable("run.py")],
    )

采纳答案by Martin Tournoij

You can work around this error by setting the environment variables manually:

您可以通过手动设置环境变量来解决此错误:

set TCL_LIBRARY=C:\Program Files\Python35-32\tcl\tcl8.6
set TK_LIBRARY=C:\Program Files\Python35-32\tcl\tk8.6

You can also do that in the setup.pyscript:

您也可以在setup.py脚本中执行此操作:

os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tk8.6'

setup([..])


But I found that actually running the program doesn't work. On the cx_freeze mailinglist it was mentioned:

但我发现实际运行该程序不起作用。在cx_freeze 邮件列表中提到

I have looked into it already and no, it is not just a simple recompile -- or it would have been done already! :-)

It is in progress and it looks like it will take a bit of effort. Some of the code in place to handle things like extension modules inside packages is falling over -- and that may be better solved by dropping that code and forcing the package outside the zip file (another pull request that needs to be absorbed). I should have some time next week and the week following to look into this further. So all things working out well I should put out a new version of cx_Freeze before the end of the year.

我已经研究过它,不,它不仅仅是一个简单的重新编译——或者它已经完成了!:-)

它正在进行中,看起来需要一些努力。一些用于处理包内的扩展模块之类的代码正在崩溃 - 通过删除该代码并强制将包放在 zip 文件之外(另一个需要吸收的拉取请求)可以更好地解决这个问题。下周和下周我应该有时间进一步研究这个问题。所以一切顺利,我应该在年底之前推出一个新版本的 cx_Freeze。

But perhaps you have more luck ... Here's the bug report.

但也许你有更多的运气......这是错误报告

回答by Guilherme

Just put this before the setup at setup.py

只需将其放在 setup.py 的设置之前

import os

os.environ['TCL_LIBRARY'] = "C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\LOCAL_TO_PYTHON\Python35-32\tcl\tk8.6"

And run it:

并运行它:

python setup.py bdist_msi

This worked fine for me.

这对我来说很好。

回答by Ben Wills

If you get following error with python 3.6:

如果您在使用 python 3.6 时遇到以下错误:

copying C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6 -> build\exe.win-amd64-3.6\tcl
error: [Errno 2] No such file or directory: 'C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6'

Simply create LOCAL_TO_PYTHONdir in C:\then create Python35-32dir inside it. Now copy tcldir from existing Python36dir (in C:\) into Python35-32.

只需在其中创建LOCAL_TO_PYTHONdir,C:\然后Python35-32在其中创建dir。现在将tcldir 从现有Python36目录 (in C:\)复制到Python35-32.

Then it works fine.

然后它工作正常。

回答by D. L. Müller

Instead of setting the environment variables using installation specific absolute paths like C:\\LOCAL_TO_PYTHON\\...you may also derive the necessary paths dynamically using the __file__attribute of Python standard package like os:

除了使用特定于安装的绝对路径设置环境变量之外,C:\\LOCAL_TO_PYTHON\\...您还可以使用__file__Python 标准包的属性动态派生必要的路径,例如os

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')

After this fix the executable file will be created, but you will probably get a "DLL not found error" when you try to execute it - at least with Python 3.5.3 and cx_Freeze 5.0.1 on Windows 10.

在此修复之后,将创建可执行文件,但是当您尝试执行它时,您可能会收到“未找到 DLL 错误” - 至少在 Windows 10 上使用 Python 3.5.3 和 cx_Freeze 5.0.1。

When you add the following options, the necessary DLL-files will be copied automatically from the Python-Installation directory to the build-output of cx-Freeze and you should be able to run your Tcl/Tk application:

当您添加以下选项时,必要的 DLL 文件将自动从 Python 安装目录复制到 cx-Freeze 的构建输出,您应该能够运行您的 Tcl/Tk 应用程序:

options = {
    'build_exe': {
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
         ],
    },
}

# ...

setup(options = options,
      # ...
)

回答by Onur ?ak?r

If you get following error with python 3.6:

如果您在使用 python 3.6 时遇到以下错误:

copying C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6-> build\exe.win-amd64-3.6\tclerror: [Errno 2] No such file or directory: 'C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6'

复制C:\LOCAL_TO_PYTHON\Python35-32\tcl\tcl8.6->build\exe.win-amd64-3.6\tcl错误:[Errno 2] No such file or directory: 'C:\\LOCAL_TO_PYTHON\\Python35-32\\tcl\\tcl8.6'

Simply create LOCAL_TO_PYTHON dir in C:\ then create Python35-32 dir inside it. Now copy tcl dir from existing Python36 dir (in C:) into Python35-32.

只需在 C:\ 中创建 LOCAL_TO_PYTHON 目录,然后在其中创建 Python35-32 目录。现在将 tcl 目录从现有的 Python36 目录(在 C: 中)复制到 Python35-32。

Then it works fine.

然后它工作正常。

**I did this steps and created a .exe file into the build dir but if ? try to click app dont wait on the screen instantly quick, my codes here **

**我执行了这些步骤并在构建目录中创建了一个 .exe 文件,但是如果?尝试单击应用程序不要立即在屏幕上等待,我的代码在这里**

from tkinter import *
import socket



window=Tk()
window.geometry("400x150")
window.title("IpConfiger")
window.config(background="black")

def goster():
    x=socket.gethostbyname(socket.gethostname())
    label=Label(window,text=x,fg="green",font=("Helvetica",16))
    label.pack()
def information():
    info=Label(window,text="Bu program anl?k ip de?erini 
    bast?r?r.",fg="green",font=("Helvetica",16),bg="black")
    info.pack()


information()
tikla=Button(window,text="ip g?ster",command=goster)

tikla.pack()

回答by jpeg

D. L. Müller's answerneed to be modified for cx_Freeze version 5.1.1 or 5.1.0. In these versions of cx_Freeze, packages get frozen into a subdirectory libof the build directory. The TCL and TK DLLs need to be moved there as well. This can be achieved by passing a tuple (source, destination)to the corresponding entry of the include_fileslist option (see the cx_Freeze documentation).

DL Müller 的回答需要针对 cx_Freeze 版本 5.1.1 或 5.1.0 进行修改。在这些版本的 cx_Freeze 中,包被冻结到lib构建目录的子目录中。TCL 和 TK DLL 也需要移到那里。这可以通过将元组传递给列表选项(source, destination)的相应条目来实现include_files(请参阅cx_Freeze 文档)。

Altogether the setup.pyscript needs to be modified as follows:

setup.py脚本总共需要修改如下:

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')

# ...

options = {
    'build_exe': {
        'include_files':[
            (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll'))
            (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))
         ],
    },
}

# ...

setup(options = options,
      # ...
)

回答by Raoul HATTERER

The initial KeyError problem:

最初的 KeyError 问题:

KeyError

密钥错误

This worked for me with python 3.7 on windows 7:

这对我在 Windows 7 上使用 python 3.7 有用:

from cx_Freeze import setup, Executable
import os
import sys

where = os.path.dirname(sys.executable)


os.environ['TCL_LIBRARY'] = where+"\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = where+"\tcl\tk8.6"

build_exe_options = {"include_files": [where+"\DLLs\tcl86t.dll", where+"\DLLs\tk86t.dll"]}  


setup(
    name = "SudoCool",
    version = "0.1",
    description = "Programme de SUDOKU",
    options={"build_exe": build_exe_options},  
    executables = [Executable("sudoku.py")]
) 

Now cx_Freeze is working: it's working

现在 cx_Freeze 正在工作: 它在工作

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

My application is working: enter image description here

我的应用程序正在运行: 在此处输入图片说明