如何在Lambda上导入并正确运行pip导入的库? [英] How can i import and properly run pip imported libraries on lambda?

查看:217
本文介绍了如何在Lambda上导入并正确运行pip导入的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为python中的lambda函数导入了boto3.当我测试lambda时,出现此错误:No module named boto3,这是我们所期望的. 然后,我将这些文档准确地称为

I imported boto3 for my lambda function in python. When i test lambda it gives this error : No module named boto3 which is rather expected. Then i referred to the docs,to this link to be exact https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html and realized i need to create a deployment package the issue is i didn't understand the docs well enough to keep up and would like them to be explained again in a simpler way by someone that has already done the required steps.

推荐答案

您需要在本地系统中创建一个文件夹,在该文件夹中安装所需的库,压缩该文件夹的内容,然后将压缩后的文件上传到AWS Lambda.

You need to make a folder in your local system, install the required libraries into that folder, zip the contents of the folder and upload the zipped file to the AWS Lambda.

  • 在本地系统中创建文件夹

我认为您不需要这方面的帮助.假设您在D驱动器中创建了一个名为yellow-bot的文件夹

I don't think you need help in this. Lets suppose you made a folder in D drive named yellow-bot

  • 将所需的库安装到文件夹中

您可以使用以下命令在文件夹中安装所需的软件包

You can install the required packages in the folder using below command

    pip install {package-name} -t "{path-to-project-dir}"

在您的情况下,它将是:

In your case it would be:

    pip install apiai -t "D:\yellow-bot"

  • 压缩文件夹的内容
    • Zip the contents of the folder
    • 现在,在安装了必需的库之后,您的yellow-bot文件夹中将有多个文件和文件夹.您需要全选并压缩内容. 请注意,请勿压缩文件夹,而应进入文件夹并压缩内容.
      就像下面的屏幕截图一样.

      Now after installing the required libraries there will be multiple files and folders in your yellow-bot folder. You need to select all and zip the content. Please note that do not zip the folder, instead you go inside the folder and zip the contents.
      It would be something like below screenshot.

      • 创建lambda函数并上传zip

      现在转到AWS Lambda,创建一个lambda函数,给出正确的运行时等等.然后将select upload zip文件保存在code entry type中.选择您的邮政编码,然后单击上载.
      确保提供正确的Handler.
      它遵循以下命名约定:

      Now go to AWS Lambda, create a lambda function, give correct run-time and all that. Then select upload zip file in code entry type. Select your zip and click on upload.
      Make sure to give correct Handler.
      It follows naming convention as:

      函数中的filename.handler-method值.例如, "main.handler"将调用main.py中定义的处理程序方法.

      The filename.handler-method value in your function. For example, "main.handler" would call the handler method defined in main.py.

      由于在这种情况下,我已经上传了connector.py文件,并且输入功能称为lambda_handler(),因此正确的Handler应为connector.lambda_handler

      Since in this case I have uploaded connector.py file and entry function was called lambda_handler() so correct Handler would be connector.lambda_handler

      单击Save,您已完成.

      希望有帮助.

      这篇关于如何在Lambda上导入并正确运行pip导入的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆