Python 无法在 AWS Lambda 上使用 Requests-Module
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40741282/
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
Cannot use Requests-Module on AWS Lambda
提问by codepleb
I need to do a rest-call within a python script, that runs once per day. I can't pack the "requests" package into my python-package using the AWS Lambdas. I get the error: "Unable to import module 'lambda_function': No module named lambda_function"
我需要在每天运行一次的 python 脚本中进行休息调用。我无法使用 AWS Lambdas 将“请求”包打包到我的 python 包中。我收到错误消息:“无法导入模块‘lambda_function’:没有名为 lambda_function 的模块”
I broke it down to the hello_world predefined script. I can pack it into a zip and upload it. Everything works. As soon as I put "import requests" into the file, I get this error.
我将其分解为 hello_world 预定义脚本。我可以将其打包成 zip 并上传。一切正常。一旦我将“导入请求”放入文件中,就会出现此错误。
Here is what I already did:
这是我已经做的:
- The permissions of the zip and the project folder (including subfolders) are set to `chmod 777`. So permissions shouldn't be a problem.
- The script itself is within the root folder. When you open the zip file, you directly see it.
- I installed the requests package into the root-folder of the project using `sudo pip install requests -t PATH_TO_ROOT_FOLDER`
- zip 和项目文件夹(包括子文件夹)的权限设置为 `chmod 777`。所以权限应该不是问题。
- 脚本本身位于根文件夹中。当您打开zip文件时,您可以直接看到它。
- 我使用`sudo pip install requests -t PATH_TO_ROOT_FOLDER`将请求包安装到项目的根文件夹中
The naming of everything looks like this:
一切的命名看起来像这样:
- zip-file: lambda_function.zip
- py-file: lambda_function.py
- handler method: lambda_handler(event, context)
- handler-definition in the "webconfig: lambda_function.lambda_handler
- 压缩文件:lambda_function.zip
- py 文件:lambda_function.py
- 处理程序方法:lambda_handler(event, context)
- “webconfig: lambda_function.lambda_handler”中的处理程序定义
The file I want to run in the end looks like this:
最后我想运行的文件是这样的:
import requests
import json
def lambda_handler(event, context):
url = 'xxx.elasticbeanstalk.com/users/login'
headers = {"content-type": "application/json", "Authorization": "Basic Zxxxxxxxxx3NjxxZxxxxzcw==" }
response = requests.put(url, headers=headers, verify=False)
return 'hello lambda_handler'
I'm glad for ANY kind of help. I already used multiple hours on this issue.
我很高兴得到任何帮助。我已经在这个问题上使用了几个小时。
采纳答案by codepleb
I finally solved the problem: The structure in my zip file was broken. It is important that the python script and the packed dependencies (as folders) are in the root of the zip file. This solved my problem.
我终于解决了这个问题:我的 zip 文件中的结构被破坏了。重要的是,python 脚本和打包的依赖项(作为文件夹)位于 zip 文件的根目录中。这解决了我的问题。
It's a bit depressing if you find such easy errors after hours of try and failure.
如果您在数小时的尝试和失败后发现如此简单的错误,这有点令人沮丧。
回答by Sining Liu
EDIT: On Oct-21-2019 Botocore removed the vendored version of requests: https://github.com/boto/botocore/pull/1829.
编辑:在 2019 年 10 月 21 日,Botocore 删除了请求的供应商版本:https: //github.com/boto/botocore/pull/1829。
To use requests module, you can simply import requests
from botocore.vendored
. For example:
要使用 requests 模块,您可以简单地requests
从botocore.vendored
. 例如:
from botocore.vendored import requests
def lambda_handler(event, context):
response = requests.get("https://example.com/")
print response.json()
you can see this gistto know more modules that can be imported directly in AWS lambda
您可以查看此要点以了解更多可以直接在 AWS lambda 中导入的模块
回答by Pramod Munemanik
I believe you have lambda_function.py
on the Lambda console. You need to first create the Lambda function deployment package, and then use the console to upload the package.
我相信你已经lambda_function.py
在 Lambda 控制台上。您需要先创建 Lambda 函数部署包,然后使用控制台上传包。
- You create a directory, for example
project-dir
on your system (locally) - create
lambda_function.py
inproject-dir
, copy the content oflambda_function.py
from lambda console and paste it inproject-dir/lambda_function.py
pip install requests -t /path/to/project-dir
- Zip the content of the
project-dir
directory, which is your deployment package (Zip the directory content, not the directory)
- 您创建一个目录,例如
project-dir
在您的系统上(本地) - create
lambda_function.py
inproject-dir
,lambda_function.py
从 lambda 控制台复制内容并将其粘贴到project-dir/lambda_function.py
pip install requests -t /path/to/project-dir
- 压缩
project-dir
目录内容,也就是你的部署包(压缩目录内容,不是目录)
Go to the Lambda console, select upload zip file in code entry type and upload your deployment package. Import requests should work without any error.
转到 Lambda 控制台,在代码条目类型中选择上传 zip 文件并上传您的部署包。导入请求应该可以正常工作而不会出现任何错误。
回答by Diego Tejada
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
回答by qarly_blue
With this command download the folder package
使用此命令下载文件夹包
pip install requests -t .
Run this command on your local machine, then zip your working directory, then upload to aws.
在您的本地机器上运行此命令,然后压缩您的工作目录,然后上传到 aws。