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

Smartsheet API的get_cell_history方法在Python正常,转exe后报错

问题:PyInstaller打包Python脚本为exe后报错“'str'对象没有'get_cell_history'属性”

用户原代码:

lst_Incor = []
for i in range(250):
    if get_cell_value_by_column_id(sheet_Source.rows[i],450382123898xxxx) == 'Incorporating US Markup':
        response = smart.Cells.get_cell_history(
          SheetId_Source,       # sheet_id
          sheet_Source.rows[i].id,       # row_id
          450382123898xxxx,       # column_id
          include_all=True)
        revisions = response.data
        lst_Incor.append(str(revisions[0].modified_at)[:10])
    else:
        lst_Incor.append('')
print(lst_Incor)

问题描述

上述代码在Python环境中运行正常,但通过PyInstaller转换为exe文件后无法运行,控制台报错提示“'str'对象没有'get_cell_history'属性”。

解决方案

  • 检查smart对象初始化逻辑:PyInstaller打包可能改变模块导入或对象初始化方式,导致smart.Cells被错误解析为字符串而非预期的API客户端对象。确认打包后smart对象是否正确初始化,比如依赖的配置文件是否被打包、初始化代码能否正常执行。
  • 显式指定依赖模块:PyInstaller有时无法自动识别第三方库的隐式依赖,可在打包命令中用--hidden-import参数指定smart相关模块:
    pyinstaller --hidden-import smart --hidden-import smart.Cells your_script.py
    
  • 验证对象类型:在调用get_cell_history前添加类型检查,确认smart.Cells的实际类型:
    print(type(smart.Cells))
    
    运行打包后的exe查看输出,若结果为<class 'str'>,需排查smart模块初始化过程,是否存在变量名冲突或配置读取错误导致Cells被赋值为字符串。
  • 用--onedir模式测试:先尝试单目录模式打包(而非单文件--onefile),便于查看打包后的文件结构,确认依赖库是否被正确包含、smart模块文件是否存在。
  • 排查API客户端序列化问题:如果smart是API客户端库,打包后可能存在对象序列化异常,尝试重新初始化smart.Cells对象,确保在打包环境中能正确创建客户端实例,不要依赖打包前的初始化状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 05:55:02