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

2

7

I have made a very simple Python helper, to update a task in Asana with custom field on a task. it works on my local machine in terminal.

I am trying to add it to a Zapier 'Run Python' block, but get what looks like a generic error 'str' object has no attribute 'copy'

Here's the Python code which I'd appreciate any advice on why it wont run in a "Run Python" module in Zapier -- there's no str in these lines!!?

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'
Glynisglynn answered 3/5, 2021 at 15:6 Comment(6)
Did you forget to include the relevant code block?Supporter
You need to include the reproducable code. However, the error is self explanatory. You are trying to run a function, copy, on a variable that has a string value and not an object value.Supporter
@JustinOberle thats the entire code! it works in Visual Studio terminal, and my OS X terminal. Zapier tells me to start a "Run Python" code block as a step -- and to paste my code in there. im not trying to run any copy function. i guess Zapiers parser is? zapier.com/help/create/code-webhooks/use-python-code-in-zapsGlynisglynn
There is no possible way that is the entire code. When I copy and paste it into my editor I have syntax errors on basically every line saying certain things are not defined. for example, where is input_data defined?Supporter
I appreciate your replies but i am following this advice from the Zapier guide; If you encounter a problem, we recommend asking questions on StackOverflow and tagging them with "Zapier". Seems you're not familiar with the Zapier code block "Run Python". The input_data[] comes from that.Glynisglynn
Yup, no idea what that is. I will leave this for someone else. Good luck!Supporter
S
13

I realize this is answered, but I wanted to add some context. Code by Zapier steps expect a dict to be returned; you're returning a string.

Zapier should throw a more explicit error here (something like "expected dict, got str"), but isn't. Instead, it's calling .copy() on the output, which results in the error you're seeing:

>>> {}.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'

There are two options to fix it:

  1. Set a key on the pre-defined output dict (the currently accepted answer)
  2. Manually return a dict: return {'field_name': 'task #' + input_data['row_number'] + 'assigned'}

Either will work here.

Shanghai answered 13/5, 2021 at 19:33 Comment(2)
came back to my question as I was having trouble with another "Run Code" Python / Zapier block, this helped me even more this time, thansk very much DavidGlynisglynn
Glad to hear it!Shanghai
I
3

I had better luck with the following syntax (with your code in place):

output['outputfieldname'] = 'task #' + input_data['row_number'] + 'assigned'
Iodize answered 4/5, 2021 at 19:45 Comment(1)
thanks so much. the Asana guide for this module should be much clearer about the necessity for an output variable!Glynisglynn

© 2022 - 2024 — McMap. All rights reserved.