简体   繁体   English

AWS Lambda - 无法导入模块“main”:没有名为“psycopg2”的模块

[英]AWS Lambda - Unable to import module 'main': No module named 'psycopg2'

I'm trying to run my script on my local machine using this command sam local invoke MyFunction but I got this error message.我正在尝试使用此命令sam local invoke MyFunction在我的本地计算机上运行我的脚本,但我收到了此错误消息。

Invoking main.lambda_handler (python3.8)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.8:rapid-1.22.0.
START RequestId: 56f2ad8a-ff9b-49cb-87f0-fda8723627ac Version: $LATEST
Traceback (most recent call last): Unable to import module 'main': No module named 'psycopg2'
END RequestId: 56f2ad8a-ff9b-49cb-87f0-fda8723627ac
REPORT RequestId: 56f2ad8a-ff9b-49cb-87f0-fda8723627ac  Init Duration: 0.23 ms Duration: 91.34 ms                                                   Billed Duration: 100 ms  Memory Size: 128 MB     Max Memory Used: 128 MB        
{"errorMessage": "Unable to import module 'main': No module named 'psycopg2'", "errorType": "Runtime.ImportModuleError", "stackTrace": []}

I already cpied and put the library into the AWS Lambda project folder.我已经 cpied 并将库放入 AWS Lambda 项目文件夹中。

在此处输入图像描述

You need to copy the psycopg2 library in Lambda project folder under .aws-sam/build directory.您需要复制.aws-sam/build目录下 Lambda 项目文件夹中的 psycopg2 库。

Although, the best way would be to provide requirements.txt file in your lambda project folder and mention your dependencies in there.虽然,最好的方法是在您的 lambda 项目文件夹中提供requirements.txt文件,并在其中提及您的依赖项。 Then the sam build command will download and copy the dependencies in .aws-sam/build directory for you.然后sam build命令将为您下载并复制.aws-sam/build目录中的依赖项。

Without the entry in requirements.txt file, you will loose manually copied files uder .aws-sam/build directory on running subsequent sam build command.如果没有 requirements.txt 文件中的条目,您将在运行后续sam build命令时丢失手动复制的文件.aws-sam/build目录。

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

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