You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

cx_Freeze打包报错AttributeError: 'list' object has no attribute 'items'如何解决

错误原因

你代码中setup函数的options参数配置存在三处核心问题:

  • build_exe对应的配置项必须是字典类型,你直接传入了include_files列表,类型不匹配触发报错
  • 配置逻辑冲突:你既把tkinter加入了排除列表,又主动引入tk相关的dll文件,二者互斥
  • 预先定义的build_exe_options配置字典未被实际使用

解决方案

根据你的程序是否依赖tkinter选择对应修正方案:

方案1:程序不依赖tkinter(绝大多数蛇类游戏用pygame开发,不需要tkinter)

直接移除tk相关dll的引入逻辑,修正配置项赋值即可,修正后的完整代码:

import sys, os
from cx_Freeze import setup, Executable

# 依赖配置可按需调整
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

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

setup(
    name = "Snake",
    version = "0.1",
    description = "A Classic Snake Game with a few Modifications!",
    options = {"build_exe": build_exe_options},
    executables = [Executable("main.py", base=base)]
)

方案2:程序确实依赖tkinter

将include_files合并到配置字典中,移除tkinter的排除规则,修正后的完整代码:

import sys, os
from cx_Freeze import setup, Executable

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
build_exe_options = {
    "packages": ["os", "tkinter"],
    "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'))
    ]
}

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

setup(
    name = "Snake",
    version = "0.1",
    description = "A Classic Snake Game with a few Modifications!",
    options = {"build_exe": build_exe_options},
    executables = [Executable("main.py", base=base)]
)

修正完成后重新运行python setup.py build即可正常打包。

内容的提问来源于stack exchange,提问作者finn fynzer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 13:54:03