问题描述
我最近开始使用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"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!