aws lambda 无法导入模块“lambda_function":没有名为“requests"的模块 [英] aws lambda Unable to import module 'lambda_function': No module named 'requests'

查看:29
本文介绍了aws lambda 无法导入模块“lambda_function":没有名为“requests"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 AWS Lambda 来针对我编写的一些 Python 代码使用触发器.我目前有 2 个 lambda 函数,它们都是用 ZIP 文件创建的.我创建的第二个应该是测试触发事件.

I have recently started to use AWS Lambda to use triggers against some python code I have written. I currently have 2 lambda functions, both of which have been created with ZIP files. The second one I created is supposed to test the trigger events.

这是为了测试目的,所以我使用的是最好的代码:

This is for testing purposes so I'm using the best code of all:

def lambda_handler(event, context):
    print ("Hello World")

但是,我又得到了这个错误:

However, I get this error back:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Request ID:
"65024f16-172c-11e8-ab26-27ff3322e597"

Function Logs:
START RequestId: 65024f16-172c-11e8-ab26-27ff3322e597 Version: $LATEST
Unable to import module 'lambda_function': No module named 'requests'

END RequestId: 65024f16-172c-11e8-ab26-27ff3322e597
REPORT RequestId: 65024f16-172c-11e8-ab26-27ff3322e597  Duration: 15.93 ms  
Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 22 MB  

在我搜索过的任何地方,通过确保函数名称正确或确保 .zip 文件可读来解决问题.我满足了这两个条件(文件名是 lambda_function.py 并且它在根目录中).

Everywhere I have searched for this, the answer was solved by making sure the names for the functions were correct or making sure the .zip file was readable. I have satisfied both of these conditions (the name of the file is lambda_function.py and it is in the root).

或者,这似乎是日志的问题.我仔细检查了我的许可,我有能力使用所有资源创建日志.任何其他想法可能是什么问题?

Alternatively, it seems like it might be an issue with the logs. I double checked my permission and I have the ability to create logs with all resources. Any other ideas what the issue might be?

推荐答案

requests 库在 lambda 中默认不提供.看起来您正试图将它导入到您的函数/库中的某处.要导入它,您需要以下行:

requests library doesn't come by default in lambda. It looks like you are trying to import it in your function / library somewhere. To import it, you need the following line:

from botocore.vendored import requests

或者,您需要将 requests 库压缩到 zip 文件的根目录中.

Alternatively, you would need to zip the requests library in the root of your zip file.

您的某个库中可能存在需要此功能的依赖项.要解决这个问题,请在您的应用程序 zip 中安装 requests.为此,请在应用程序的根目录中运行以下命令:pip install requests -t ./.

There may be a dependency in one of your libraries that may need this. To overcome this, install requests in your application zip. To do this, run the following command in the root directory of your application: pip install requests -t ./.

更好的方法是创建一个名为 requirements.txt 的文件,并在其中添加所有依赖项.使用 virtualenv 安装在 requirements.txt 中定义的所有包:pip install -r requirements.txt -t ./

A better way would be to create a file called requirements.txt and add all the dependencies in there. Use virtualenv to install all the packages defined in the requirements.txt using: pip install -r requirements.txt -t ./

更新:从 19 年 10 月 21 日开始,botocore 中请求库的供应商版本将被删除.请参阅这篇博文了解更多详情.

UPDATE: Starting 10/21/19, the vendored version of the requests library in botocore will be removed. Refer this blog post for more details.

这篇关于aws lambda 无法导入模块“lambda_function":没有名为“requests"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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