PyInstaller打包CustomTkinter脚本后运行exe报AttributeError错误求助
解决CustomTkinter打包exe后AttributeError: 'customtkinter' has no attribute 'CTk'的问题
可能的原因及对应解决方案
1. 确认CustomTkinter版本兼容性
CTk是CustomTkinter 2.0及以上版本才引入的核心组件,若你使用的是2.0之前的旧版本,模块中确实不存在CTk属性。
- 执行命令升级到最新稳定版:
pip install --upgrade customtkinter - 升级后先运行脚本确认GUI正常显示,再重新尝试打包。
2. 替换手动--add-data为--collect-all参数
你当前手动指定customtkinter路径的方式容易出现路径配置错误,PyInstaller提供的--collect-all参数可自动收集目标包的所有文件,更可靠:
- 使用以下命令打包:
pyinstaller --noconfirm --onedir --windowed --collect-all customtkinter DataAnalysisTool.py
3. 手动添加隐藏导入
若PyInstaller未自动检测到customtkinter的内部依赖模块,需手动指定隐藏导入:
- 在打包命令中添加
--hidden-import customtkinter:pyinstaller --noconfirm --onedir --windowed --collect-all customtkinter --hidden-import customtkinter DataAnalysisTool.py
4. 使用.spec文件精细化配置
如果命令行参数管理不便,可生成.spec文件修改配置:
- 生成初始.spec文件:
pyinstaller --name DataAnalysisTool --onedir --windowed DataAnalysisTool.py - 打开
DataAnalysisTool.spec,修改datas和hiddenimports部分:from PyInstaller.utils.hooks import collect_all datas, binaries, hiddenimports = collect_all('customtkinter') - 用.spec文件打包:
pyinstaller --noconfirm DataAnalysisTool.spec
5. 验证最小可复现示例
确保最小示例代码正确调用CTk:
import customtkinter app = customtkinter.CTk() app.title("Test") app.mainloop()
用上述正确命令打包该示例,确认exe正常运行后,再迁移到主项目。
内容的提问来源于stack exchange,提问作者schmisi
相关产品推荐
相关产品推荐

