gpt4 book ai didi

python - AWS Lambda : Unable to import module 'python_handler' : No module named '_cffi_backend'

转载 作者:行者123 更新时间:2023-12-01 07:31:33 32 4
gpt4 key购买 nike

当我需要读取 API 上的信息、创建 CSV 文件并将其上传到 SFTP 服务器时,我正在创建 AWS Lambda 函数。

我已经在我的 venv 上安装了 paramiko,在 Windows 上使用 Ubuntu,并且 cffi 模块就像一个依赖项,但是当代码运行时,我收到此错误:

{
"errorMessage": "Unable to import module 'python_handler': No module named '_cffi_backend'",
"errorType": "Runtime.ImportModuleError"
}

按照我的代码操作:

import paramiko
import requests
from random import randrange
from datetime import datetime
from datetime import timedelta
from requests.auth import HTTPBasicAuth


def lambda_handler(event, context):
# Lambda function
addZero = lambda x : '0' + str(x) if x < 10 else str(x)

# Actual datetime
ac_yr = datetime.utcnow().year
ac_mo = datetime.utcnow().month
ac_da = datetime.utcnow().day
ac_hh = datetime.utcnow().hour
ac_mi = datetime.utcnow().minute
ac_se = datetime.utcnow().second

# 24 hours ago
ag_yr = (datetime.utcnow() - timedelta(hours=24)).year
ag_mo = (datetime.utcnow() - timedelta(hours=24)).month
ag_da = (datetime.utcnow() - timedelta(hours=24)).day
ag_hh = (datetime.utcnow() - timedelta(hours=24)).hour
ag_mi = (datetime.utcnow() - timedelta(hours=24)).minute
ag_se = (datetime.utcnow() - timedelta(hours=24)).second

# API Infos
api_key = 'XYZ'
page_id = 'XYZ'

# Call API
param = {
'sort_order': 'asc',
'from': '{}-{}-{}T{}:{}:{}.000Z'.format(ag_yr, addZero(ag_mo), addZero(ag_da), addZero(ag_hh), addZero(ag_mi), addZero(ag_se)),
'to': '{}-{}-{}T{}:{}:{}.000Z'.format(ac_yr, addZero(ac_mo), addZero(ac_da), addZero(ac_hh), addZero(ac_mi), addZero(ac_se))
}
r = requests.get('https://api.unbounce.com/pages/{}/leads'.format(page_id), auth=HTTPBasicAuth(api_key, 'pass'), params=param)

# If connection it's ok
if r.status_code == 200:
# If has any result
if len(r.json()['leads']) > 0:
cont = ''
for lead in r.json()['leads']:
cont += lead['form_data']['cpf'][0] + ','
cont += lead['form_data']['nome_completo'][0] + ','
cont += lead['form_data']['email'][0]
else:
return 'Não há resultados no momento'
else:
return 'Falha na conexão'

# Write a CSV file
f = open('my_csv.csv','w')
f.write('PAC_ID, PAC_NAME, PAC_EMAIL\n')
f.write(cont)
f.close()

transport = paramiko.Transport(('host-info', 22))
transport.connect(None, 'user-info', 'password-info', None)

sftp = paramiko.SFTPClient.from_transport(transport)
sftp.chdir('Import')
sftpclient.put('my_csv.csv')

return 'OK'

知道如何解决这个问题吗?

最佳答案

最简单的解决方案是使用 lambci/lambda容器如所述here 。我写了一些详细的内容here.

简而言之,一些 Python 包(例如 cffi)具有操作系统级别的依赖关系。由于您打包代码的操作系统与 Lambda 使用的操作系统不同,因此脚本无法运行。

关于python - AWS Lambda : Unable to import module 'python_handler' : No module named '_cffi_backend' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57189352/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com