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

Zapier运行Python代码报错'str' object has no attribute 'copy'求助

Zapier中Python代码报错'str' object has no attribute 'copy'的排查与解决

问题场景

在Zapier中运行Python代码时触发错误:Failed to run your Python code 'str' object has no attribute 'copy',但相同代码在VSCode本地环境可正常执行。

代码如下:

import re
import requests
import json

# Email for search
email_to_search  = (input['Email'])

# AMOcrm
subdomain = 'DOMAIN'
user_login = 'LOGIN'
api_key = 'API'

# Parameters for authorization
params = {'USER_LOGIN': user_login, 'USER_HASH': api_key}

# URL for company search by email
company_search_url = f'https://{subdomain}.amocrm.ru/api/v4/companies?query={email_to_search}'

# Request execution
response = requests.get(company_search_url, params=params)

# Check response status and company search
if response.status_code == 200:
    company_data = response.json()
    companies = company_data.get('_embedded', {}).get('companies', [])
    if companies:
        company_id = companies[0]['id']

        # Check response status and company search
        company_info_url = f'https://{subdomain}.amocrm.ru/api/v4/companies/{company_id}?with=leads'

        # Request execution
        company_info_response = requests.get(company_info_url, params=params)

        # Checking the response status and displaying company information
        if company_info_response.status_code == 200:
            company_info_data = company_info_response.json()

            # Obtaining information on related leads
            related_leads = company_info_data.get('_embedded', {}).get('leads', [])
            if related_leads:
                # Getting the ID of the last lead
                last_lead_id = related_leads[-1]['id']
                
                # Formation and output of a link to the last lead
                last_lead_url = f"https://{subdomain}.amocrm.ru/leads/detail/{last_lead_id}"
                print(last_lead_url)
            else:
                last_lead_url = ("No related leads found")

        else:
            last_lead_url = (f"Error in fetching company info: {company_info_response.status_code}")

    else:
        last_lead_url = ("Company not found")
else:
    last_lead_url = (f"Error: {response.status_code}")

output = last_lead_url

已完成排查:

  • 确认Zapier的Zap设置中字段名称与代码预期匹配
  • 修正代码中字段名错误,确保数据提取逻辑正确
  • 验证数据格式及Zapier配置正确性

报错原因

Zapier对Python代码的output有格式要求:必须是字典类型。Zapier内部会对output执行copy()方法以处理后续流程,但字符串(str)对象没有该方法,因此触发报错。本地环境无此限制,所以代码能正常运行。

解决方案

将代码最后一行的字符串输出包装为字典格式:

修改前:

output = last_lead_url

修改后:

output = {"last_lead_url": last_lead_url}

修改后,Zapier可以正确识别输出的字典对象,执行copy()方法时不会再触发错误,后续流程也能正常读取last_lead_url字段的值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:16:23