使用py2exe编译tkinter程序遇AttributeError: 'NoneType'无rsplit属性问题
py2exe打包Tkinter程序时
AttributeError错误的解决方法 问题场景
使用py2exe编译基于Tkinter的Python程序时,触发以下错误:
Traceback (most recent call last): File "C:\Users\henzo\OneDrive\Documents\python\auto_fond\setup.py", line 27, in <module> zipfile='test.zip' File "C:\EduPython\App\lib\site-packages\py2exe\__init__.py", line 176, in freeze builder.build() File "C:\EduPython\App\lib\site-packages\py2exe\runtime.py", line 273, in build self.build_archive(libpath, delete_existing_resources=True) File "C:\EduPython\App\lib\site-packages\py2exe\runtime.py", line 479, in build_archive base = dist_path.rsplit('\\', 1)[0] AttributeError: 'NoneType' object has no attribute 'rsplit'
解决方案
修正
zipfile参数配置
错误根源是dist_path为None,大概率是zipfile参数的路径设置异常。可以:- 将
zipfile设为None,让py2exe使用默认的library.zip - 或者指定明确的可写相对路径,比如
'./test.zip'
修改后的示例代码:
from py2exe import freeze freeze( windows=[{'script': 'fichier.py'}], options={ 'bundle_files': 1, 'compressed': 1, 'dll_excludes': ['tcl85.dll', 'tk85.dll'], 'includes': ['os', 'ctypes', 'tkinter', 'PIL', 'tkinter.font'] }, zipfile=None )- 将
调整
bundle_files参数bundle_files=1的单文件打包模式可能与Tkinter的依赖dll冲突,先尝试设置bundle_files=3(默认值,生成独立文件夹)验证编译是否正常。如果成功,再考虑是否继续使用单文件模式,或保留多文件部署形式。修正Tkinter依赖dll的处理
不要随意排除tcl85.dll和tk85.dll,这些是Tkinter运行必需的文件。移除dll_excludes配置项,让py2exe自动打包必要的依赖,或者手动将这些dll复制到exe所在目录。检查环境兼容性
当前使用的EduPython可能存在版本兼容问题,建议:- 升级py2exe到最新版本:
pip install --upgrade py2exe - 尝试使用标准Python环境而非EduPython重新打包测试
- 升级py2exe到最新版本:
内容的提问来源于stack exchange,提问作者programmeurdu33
相关产品推荐
相关产品推荐

