AWS Lambda-无法导入模块"lambda_function" [英] AWS Lambda - unable to import module 'lambda_function'

查看:425
本文介绍了AWS Lambda-无法导入模块"lambda_function"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像我之前的许多人一样,我正在尝试运行一个AWS Lambda函数,当我对其进行测试时,我得到了

Like many others before me, I'm trying to run an AWS Lambda function and when I try to test it, I get

"errorMessage":无法导入模块'lambda_function'"

"errorMessage": "Unable to import module 'lambda_function'"

我的处理程序设置为lambda_function.lambda_handler,我确实有一个名为lambda_function.py的文件,其中包含一个名为lambda_handler的函数.这是屏幕截图作为证明:

My Handler is set to lambda_function.lambda_handler, and I indeed have a file named lambda_function.py which contains a function called lambda_handler. Here's a screenshot as proof:

当我在随附的IDE中内联编写代码片段时,一切工作都很好,但是当我压缩具有其所有依赖项的完整程序并将其上载时,出现了以上错误.

Everything was working fine when I was writing snippets of code inline in the included IDE, but when I zipped my full program with all of its dependencies and uploaded it, I got the above error.

我正在使用Numpy和Scipy软件包,它们非常大.我的压缩目录为34 MB,未压缩的目录为122 MB.我认为这应该没问题,因为压缩目录的限制为50 MB.似乎可以正常上传,因为我看到以下消息:

I'm using the Numpy and Scipy packages, which are quite large. My zipped directory is 34 MB, and my unzipped directory 122 MB. I think this should be fine since the limit is 50 MB for a zipped directory. It appears to be uploading fine, since I see the message:

您的Lambda函数一次性图像分类"的部署包太大,无法启用内联代码编辑.但是,您仍然可以立即调用函数.

The deployment package of your Lambda function "one-shot-image-classification" is too large to enable inline code editing. However, you can still invoke your function right now.

我已经看到一些帖子通过使用virtualenv解决了这个问题,但是我对该技术不熟悉,我不确定如何正确使用它.

I've seen that some posts solve this by using virtualenv, but I'm not familiar with that technology and I'm not sure how to use it properly.

我也看到过一些帖子说,有时依赖项具有依赖项,我可能需要包括这些依赖项,但是我不确定如何找到它.

I've also seen some posts saying that sometimes dependencies have dependencies and I may need to include those, but I'm not sure how to find this out.

这是lambda_function.py的顶部,应该足以看到我正在使用的库,并且我确实具有lambda_handler函数:

Here's the top portion of lambda_function.py, which should be enough to see the libraries I'm using and that I do indeed have a lambda_handler function:

import os
import boto3
import numpy as np
from scipy.ndimage import imread
from scipy.spatial.distance import cdist

def lambda_handler(event, context):

    s3 = boto3.resource('s3')

以下是我正在上传的目录的未压缩版本的屏幕截图:

Here a screenshot of the unzipped version of the directory I'm uploading:

如果这可能是一个问题,我还可以发布Lambda使用的策略角色.

I can also post the policy role that my Lambda is using if that could be an issue.

任何见解都值得赞赏!

更新:

这是我尝试过的一种解决方案: 1. git clone https://github.com/Miserlou/lambda-packages 2.在文档中创建一个名为new_lambda的文件夹 3.将我的lambda_function.py和numpy文件夹从lambda-packages复制到new_lambda,以及我根据文章使用Docker for AWS编译的scipy库:

Here's one solution I tried: 1. git clone https://github.com/Miserlou/lambda-packages 2. create a folder in Documents called new_lambda 3. copy my lambda_function.py and the numpy folder from the lambda-packages into new_lambda, along with the scipy library that I compiled using Docker for AWS as per the article: https://serverlesscode.com/post/scikitlearn-with-amazon-linux-container/ 4. Zip the new_lambda folder by right-clicking it and selecting 'compress'

我的结果:

无法导入模块"lambda_function":未命名模块 'lambda_function'

Unable to import module 'lambda_function': No module named 'lambda_function'

重申一下,我的文件名为lambda_function.py,并包含一个名为lambda_handler的函数,该函数接受两个参数(如上所示).此信息与在Handler中看到的信息相同,也与上面看到的信息相同.

To reiterate, my file is named lambda_function.py and contains a function called lambda_handler, which accepts two arguments (as seen above). This information matches that seen in Handler, also seen above.

如果有问题,我正在使用Mac计算机.

I am using a Mac computer, if that matters.

更新2

如果我按照上述步骤操作,而是直接选择要压缩的文件来压缩文件,然后右键单击并选择压缩",则会出现错误

If I follow the above steps but instead zip the files by directly selecting the files that I want to compress and then right clicking and selecting 'compress', I instead get the error

无法导入模块"lambda_function":无法导入名称"show_config"

Unable to import module 'lambda_function': cannot import name 'show_config'

此外,预编译的lambda软件包表示它们是为至少Python 2.7"编译的,但是我的lambda运行时是3.6.这可能是个问题吗?

Also, the precompiled lambda-packages says that they are compiled for "at least Python 2.7", but my lambda runtime is 3.6. Could this be an issue?

推荐答案

解决方案是从 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆