使用cx_Freeze打包Pygame为.exe时遇‘list’无‘items’属性错误
解决cx_Freeze打包Pygame项目时的
'list' object has no attribute 'items'错误 这个报错我之前帮朋友排查过,大概率是你的setup.py里参数格式写错了——尤其是options部分的结构不符合cx_Freeze的要求。
问题还原
你执行py setup.py build时触发报错,核心提示为cx_Freeze 'list' object has no attribute 'items',这说明cx_Freeze在尝试调用字典的items()方法时,拿到的却是一个列表类型的参数。
最常见的错误原因&修复方法
cx_Freeze的setup()函数里,options参数要求是嵌套字典结构,但很多新手会误写成列表。比如:
❌ 错误写法
# 错误:把options写成了列表,而非嵌套字典 options = ["build_exe", {"packages": ["pygame"]}]
✅ 正确写法
options = { "build_exe": { "packages": ["pygame"], # 打包需要包含的依赖包 "include_files": ["assets/", "sounds/"], # 游戏需要的资源文件/文件夹 "excludes": ["tkinter"] # 排除不需要的冗余模块 } }
完整的可用setup.py示例
给你一个适配Pygame项目的标准模板,直接替换成你的项目信息即可:
import cx_Freeze import sys # 针对Windows平台的GUI程序配置,避免打包后弹出控制台窗口 base = None if sys.platform == "win32": base = "Win32GUI" # 指定要打包的主程序文件 executables = [cx_Freeze.Executable("CattwalkV3C.py", base=base)] # 打包配置选项(必须是嵌套字典) options = { "build_exe": { "packages": ["pygame"], "include_files": ["sprites/", "backgrounds/", "audio/"], # 替换成你的资源路径 "excludes": ["unittest", "tkinter"] } } # 执行打包 cx_Freeze.setup( name="CattwalkV3C", version="1.0", description="你的Pygame游戏名称", options=options, executables=executables )
额外检查点
如果上面的修复没用,再排查这两个地方:
- 确认
executables列表里的cx_Freeze.Executable()参数没有写错,比如误把shortcut_name之类的参数传成了列表 - 检查
cx_Freeze.setup()的其他参数,有没有把本该是字典的参数传成了列表
内容的提问来源于stack exchange,提问作者Josh Wright
相关产品推荐
相关产品推荐

