"Run Python" module gives error: 'str' object has no attribute 'copy'

"Run Python" module gives error: 'str' object has no attribute 'copy'

我制作了一个非常简单的 Python 助手,用于使用任务的自定义字段更新 Asana 中的任务。它在我的本地机器上的终端上运行。

我正在尝试将它添加到 Zapier 'Run Python' 块中,但得到了一个看起来像一般性错误的东西 'str' object has no attribute 'copy'

这是 Python 代码,我很感激任何关于为什么它不会 运行 在 Zapier 的“运行 Python”模块中的建议——没有这些行中的 str!!?

import requests

headers = {'Authorization':'Bearer 1/xxxxx'}
task_id = input_data['task_id']
data = {"data": {"custom_fields": {"1200278184463303":"#" + input_data['row_number']}}}

response = requests.put('https://app.asana.com/api/1.0/tasks/' + task_id, headers=headers, json=data)

return 'task #' + input_data['row_number'] + 'assigned'

如果使用以下语法(使用您的代码),我运气会更好:

output['outputfieldname'] = 'task #' + input_data['row_number'] + 'assigned'

我知道这个问题已经得到解答,但我想添加一些上下文。 Code by Zapier 步期望 dict 被 return 编辑;你正在 return 字符串。

Zapier 应该 在这里抛出一个更明确的错误(类似于“expected dict, got str),但不是。相反,它是在输出上调用 .copy(),这会导致您看到的错误:

>>> {}.copy()
{}

>>> ''.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'copy'

'str' object has no attribute 'copy'

有两种修复方法:

  1. 在预定义的 output 字典(当前接受的答案)上设置键
  2. 手动return一个字典:return {'field_name: 'task #' + input_data['row_number'] + 'assigned'}

两者都可以在这里工作。