Python aws lambda 无法导入模块“lambda_function”:没有名为“requests”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48912253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
aws lambda Unable to import module 'lambda_function': No module named 'requests'
提问by mifin
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.
我最近开始使用 AWS Lambda 对我编写的一些 Python 代码使用触发器。我目前有 2 个 lambda 函数,它们都是用 ZIP 文件创建的。我创建的第二个应该是测试触发事件。
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
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).
在我搜索过的任何地方,通过确保函数名称正确或确保 .zip 文件可读来解决答案。我已经满足了这两个条件(文件名是 lambda_function.py 并且它在根目录中)。
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?
或者,这似乎是日志的问题。我仔细检查了我的许可,我有能力使用所有资源创建日志。任何其他想法可能是什么问题?
回答by krishna_mee2004
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:
requests
库在 lambda 中默认不出现。看起来您正试图将它导入到您的函数/库中的某处。要导入它,您需要以下行:
from botocore.vendored import requests
Alternatively, you would need to zip the requests
library in the root of your zip file.
或者,您需要将requests
库压缩到zip 文件的根目录中。
EDIT: 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 ./
.
编辑:您的某个库中可能存在需要此功能的依赖项。要解决这个问题,请安装requests
在您的应用程序 zip 中。为此,请在应用程序的根目录中运行以下命令:pip install requests -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 ./
更好的方法是创建一个名为的文件requirements.txt
并在其中添加所有依赖项。使用 virtualenv 安装在 requirements.txt 中定义的所有包:pip install -r requirements.txt -t ./
UPDATE: Starting 10/21/19, the vendored version of the requests library in botocore will be removed. Refer this blog postfor more details.
更新:从 19 年 10 月 21 日开始,botocore 中请求库的供应商版本将被删除。有关更多详细信息,请参阅此博客文章。
回答by Shubham Gorlewar
https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/
https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/
AWS removed the vendored version of requests
from Botocore.
AWSrequests
从 Botocore 中删除了供应商版本。
Steps:
脚步:
cmd >>
pip install requests
Python code:
import requests response = requests.get('https://...')
指令 >>
pip install requests
蟒蛇代码:
import requests response = requests.get('https://...')
回答by Diego Tejada
Give it a check to this answer
给它一个检查这个答案
If you're working with Python on AWS Lambda, and need to use requests, you better use urllib3, it is currently supported on AWS Lambda and you can import it directly, check the example on urllib3 site.
如果您在 AWS Lambda 上使用 Python,并且需要使用requests,则最好使用urllib3,AWS Lambda 目前支持它,您可以直接导入它,请查看 urllib3 站点上的示例。
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')
r.data
# b'User-agent: *\nDisallow: /deny\n'
r.status
# 200