简体   繁体   English

本地调用 AWS SAM(lambda) python 代码时出现“没有名为“请求”的模块”错误

[英]'No module named 'requests'' error when invoking AWS SAM(lambda) python code locally

I have recently started using AWS SAM to build API and AWS Lambda.我最近开始使用 AWS SAM 构建 API 和 AWS Lambda。 The code is working fine in AWS but I'm having some difficulty setting the local testing and debugging (which is one of the main reasons why I wanted to use SAM in the first place).该代码在 AWS 中运行良好,但我在设置本地测试和调试时遇到了一些困难(这是我首先想使用 SAM 的主要原因之一)。

The lambda_handler function is very straightforward. lambda_handler function 非常简单。 It looks similar to below.它看起来类似于下面。 I am simply running my function like some_function_here which is using 'requests' module in one of the dependent functions.我只是像 some_function_here 一样运行我的 function,它在依赖函数之一中使用“请求”模块。 In my requirements.txt in the SAM project, I have 'requests' alongside some other dependencies.在 SAM 项目的 requirements.txt 中,我有“请求”以及其他一些依赖项。 This seems to do it's job since I can see the request being installed in AWS Lambda (screenshot below).这似乎完成了它的工作,因为我可以看到请求被安装在 AWS Lambda 中(下面的屏幕截图)。

The trouble surprisingly is when I'm running SAM locally (on VsCode, I hit F5).令人惊讶的是,当我在本地运行 SAM 时(在 VsCode 上,我按 F5)。 When I run sam local invoke (since I don't need an event), I get an error sayng 'no module named requests' when it should've downloaded the requets based on the 'requirements.txt' as it's doing on the AWS Cloud.当我运行sam local invoke时(因为我不需要事件),当它应该根据 'requirements.txt' 下载请求时,我得到一个错误,说'没有名为请求的模块',就像它在 AWS 上所做的那样云。 Any suggestion is appreciated.任何建议表示赞赏。

AWS Lambda Folder Structure AWS Lambda 文件夹结构

Lambda Handler Code Lambda 处理程序代码

def lambda_handler(event, context):
    try:
        some_function_here()
        return {
            "statusCode": 200,
            "body": json.dumps({
                "message": "Job ran successfully."
            }),
        }
    except Exception as e:
        return {
            "statusCode": 500,
            "body": json.dumps({
                "message": "Something went wrong!"
            }),
        }

Error错误

(base) anojshrestha% sam local invoke
Invoking app.lambda_handler (python3.8)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.8:rapid-1.7.0.

Mounting /... as /var/task:ro,delegated inside runtime container
START RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'requests'
END RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d
REPORT RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d  Init Duration: 188.03 ms       Duration: 3.89 ms        Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 24 MB

{"errorType":"Runtime.ImportModuleError","errorMessage":"Unable to import module 'app': No module named 'requests'"}

I noticed that the Python interpreter you are using is " python3.8 ", but the module " requests " is installed in "anaconda3/lib/ python3.7 ".我注意到您使用的 Python 解释器是“ python3.8 ”,但模块“ requests ”安装在“anaconda3/lib/ python3.7 ”中。 It is recommended that you switch the Python interpreter in VSCode to " Python3.7(conda) ".建议您将 VSCode 中的 Python 解释器切换为“ Python3.7(conda) ”。 (Please click the Python interpreter in the lower left corner of VSCode to switch it.) (请点击VSCode左下角的Python解释器进行切换。)

We can use the command " python --version " to check the Python interpreter version currently used by the VSCode terminal:我们可以使用命令“ python --version ”来查看VSCode终端当前使用的Python解释器版本:

在此处输入图像描述

在此处输入图像描述

Reference: Environment in VSCode .参考: VSCode 中的环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM