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

使用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'

解决方案

  1. 修正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
    )
    
  2. 调整bundle_files参数
    bundle_files=1的单文件打包模式可能与Tkinter的依赖dll冲突,先尝试设置bundle_files=3(默认值,生成独立文件夹)验证编译是否正常。如果成功,再考虑是否继续使用单文件模式,或保留多文件部署形式。

  3. 修正Tkinter依赖dll的处理
    不要随意排除tcl85.dll和tk85.dll,这些是Tkinter运行必需的文件。移除dll_excludes配置项,让py2exe自动打包必要的依赖,或者手动将这些dll复制到exe所在目录。

  4. 检查环境兼容性
    当前使用的EduPython可能存在版本兼容问题,建议:

    • 升级py2exe到最新版本:pip install --upgrade py2exe
    • 尝试使用标准Python环境而非EduPython重新打包测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:32:56