【问题标题】:Unable to import module 'lambda_function': No module named 'sendgrid'无法导入模块“lambda_function”:没有名为“sendgrid”的模块
【发布时间】:2019-08-20 18:16:58
【问题描述】:

我正在使用 AWS Lambda、Python 3.7 和 SendGrid API 并想发送电子邮件但收到错误:

"Unable to import module 'lambda_function': No module named 'sendgrid'"

有没有办法解决这个问题?我可以看到在一些类似的问题中,模块会从某个地方导入,但无法从哪里解决。

我的 lambda 代码只是来自 SendGrid 网站的示例代码,其中的值已更新为我要使用的值:

import json
import sendgrid
import os
from sendgrid.helpers.mail import *

def lambda_handler(event, context):

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = Email("test@example.com")
    to_email = Email("****")
    subject = "Sending with SendGrid is Fun"
    content = Content("text/plain", "and easy to do anywhere, even with Python")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())

    print(response.status_code)
    print(response.body)
    print(response.headers)

谢谢

【问题讨论】:

    标签: python-3.x aws-lambda sendgrid sendgrid-api-v3


    【解决方案1】:

    lambda 环境没有可供您的代码调用的 sendgrid 模块。为了使用不属于 aws sdk 或语言的依赖项(如 sendgrid 库),您必须在本地使用包预构建代码并上传 zip 文件。可以在此处找到示例:aws python lambda。还有另一个堆栈溢出解决了相同的问题here。第二个包含一些使上传更容易的工具。

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 2020-04-29
      • 2021-08-16
      • 2019-05-12
      • 2021-02-22
      • 2021-09-02
      • 2020-01-01
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多