使用cx-Freez时出现KeyError:“TCL-Library”

2024-04-27 19:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用cx_Freeze时,在构建pygame程序时会得到一个键错误KeyError: 'TCL_Library'。我为什么要买这个,怎么修?

我的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")],
    )

Tags: py程序png错误setupdicetclpygame
3条回答

把这个放在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"

运行它:

python setup.py bdist_msi

这对我很有效。

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

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

也可以在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([..])

但我发现实际上运行这个程序是行不通的。在cx_freeze mailinglist it was mentioned上:

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.

但也许你有更多的运气。。。Here's the bug report

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

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

修复后,将创建可执行文件,但在尝试执行时可能会出现“DLL not found error”(DLL not found error),至少在Windows 10上使用Python 3.5.3和cx_Freeze 5.0.1。

当您添加以下选项时,所需的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,
      # ...
)

相关问题 更多 >