问题描述
我有一个适用于AWS Lambda函数的Python脚本,该函数向另一个端点执行HTTP POST请求.自Python的urllib2.request
以来, https://docs.python.org/2/library/urllib2 .html ,只能处理标准application/x-www-form-urlencoded
格式的数据,我想发布JSON数据,我使用了Requests库, https://pypi.org/project/requests/2.7.0/.
I've got a Python script for an AWS Lambda function that does HTTP POST requests to another endpoint. Since Python's urllib2.request
, https://docs.python.org/2/library/urllib2.html, can only handle data in the standard application/x-www-form-urlencoded
format and I want to post JSON data, I used the Requests library, https://pypi.org/project/requests/2.7.0/.
该请求库在Python运行时环境中的AWS Lambda上不可用,因此必须通过from botocore.vendored import requests
导入.到目前为止,一切都很好.
That Requests library wasn't available at AWS Lambda in the Python runtime environment, so had to be imported via from botocore.vendored import requests
. So far, so good.
今天,我收到有关此内容的弃用警告:
Today, I get a deprecation warning on that:
DeprecationWarning: You are using the post() function from 'botocore.vendored.requests'.
This is not a public API in botocore and will be removed in the future.
Additionally, this version of requests is out of date. We recommend you install the
requests package, 'import requests' directly, and use the requests.post() function instead.
AWS在此博客文章中也提到了这一点: https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/.
This was mentioned in this blog post from AWS too: https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/.
不幸的是,将from botocore.vendored import requests
更改为import requests
会导致以下错误:
Unfortunately, changing from botocore.vendored import requests
into import requests
results in the following error:
No module named 'requests'
为什么requests
在AWS Lambda上不可用于Python运行时?以及如何使用/导入它?
Why is requests
not available for the Python runtime at AWS Lambda? And how can I use / import it?
推荐答案
如果您使用的是无服务器框架
在serverless.yml
plugins:
- serverless-python-requirements
在目录根目录下创建文件requirements.txt
At the directory root create file requirements.txt
requirements.txt
requests==2.22.0
这将安装提到的请求和软件包.
This will install the requests and packages mentioned.
这篇关于AWS Lambda上的Python:不赞成使用来自botocore.vendored的`requests`,但不提供`requests`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!