我需要在每天运行一次的 python 脚本中进行休息调用。
我无法使用 AWS Lambda 将“requests”包打包到我的 python 包中。我收到错误:“无法导入模块‘lambda_function’:没有名为 lambda_function 的模块”
我将其分解为 hello_world 预定义脚本。我可以将其打包成 zip 并上传。一切正常。一旦我将“导入请求”放入文件中,我就会收到此错误。
这是我已经做过的:
- zip 和项目文件夹(包括子文件夹)的权限设置为“chmod 777”。所以权限应该不是问题。
- 该脚本本身位于根文件夹中。当您打开 zip 文件时,您可以直接看到它。
- 我使用“sudo pip install requests -t PATH_TO_ROOT_FOLDER”将请求包安装到项目的根文件夹中
一切的命名看起来像这样:
- zip 文件:lambda_function.zip
- py文件:lambda_function.py
- 处理程序方法:lambda_handler(事件,上下文)
- “webconfig: lambda_function.lambda_handler”中的处理程序定义
我想最终运行的文件如下所示:
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'
我很高兴获得任何帮助。我已经在这个问题上花了好几个小时了。
编辑:2019 年 10 月 21 日,Botocore 删除了请求的供应版本:https://github.com/boto/botocore/pull/1829 https://github.com/boto/botocore/pull/1829.
编辑 2:(2020 年 3 月 10 日):在 AWS 开发工具包中捆绑请求模块的 Lambda 服务的弃用日期现为 2021 年 1 月 30 日。https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/ https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/
编辑 3:(2022 年 11 月 22 日):AWS 取消了弃用,以便您可以继续使用请求,如下所述。AWS Blog https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/
要使用请求模块,您只需导入requests
from botocore.vendored
。例如:
from botocore.vendored import requests
def lambda_handler(event, context):
response = requests.get("https://httpbin.org/get", timeout=10)
print(response.json())
你可以看到这个要点 https://gist.github.com/gene1wood/4a052f39490fae00e0c3#file-list_aws_lambda_modules-py-L14了解更多可以直接在AWS lambda中导入的模块。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)