KeyError:当我使用cx_Freeze时'TCL_Library'

时间:2021-10-07 18:04:19

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

当我使用cx_Freeze时,我得到一个keyerror KeyError:'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")],
    )

5 个解决方案

#1


43  

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.py script:

您也可以在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.

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

#2


27  

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:

您可以使用Python标准软件包的__file__属性(如os)动态派生必要的路径,而不是使用特定于安装的绝对路径(如C:\\ LOCAL_TO_PYTHON \\ ...)设置环境变量。

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 not found error” - 至少在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,
      # ...
)

#3


12  

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.

这对我来说很好。

#4


4  

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_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.

然后它工作正常。

#5


0  

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'

复制C:\ LOCAL_TO_PYTHON \ Python35-32 \ tcl \ tcl8.6 - > build \ exe.win-amd64-3.6 \ tcl错误:[Errno 2]没有这样的文件或目录:'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()

#1


43  

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.py script:

您也可以在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.

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

#2


27  

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:

您可以使用Python标准软件包的__file__属性(如os)动态派生必要的路径,而不是使用特定于安装的绝对路径(如C:\\ LOCAL_TO_PYTHON \\ ...)设置环境变量。

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 not found error” - 至少在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,
      # ...
)

#3


12  

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.

这对我来说很好。

#4


4  

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_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.

然后它工作正常。

#5


0  

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'

复制C:\ LOCAL_TO_PYTHON \ Python35-32 \ tcl \ tcl8.6 - > build \ exe.win-amd64-3.6 \ tcl错误:[Errno 2]没有这样的文件或目录:'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()