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

查看:33
本文介绍了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 函数one-shot-image-classification"的部署包太大,无法启用内联代码编辑.但是,您现在仍然可以调用您的函数.

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-packages2. 在 Documents 中创建一个名为 new_lambda 的文件夹3. 将我的 lambda_function.py 和 numpy 文件夹从 lambda 包复制到 new_lambda 中,以及我按照文章使用 Docker for AWS 编译的 scipy 库:https://serverlesscode.com/post/scikitlearn-with-amazon-linux-container/4. 通过右键单击 new_lambda 文件夹并选择压缩"来压缩它

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?

推荐答案

解决方案是从 这个 来源.

The solution was zipping numpy and scipy precompiled packages from this source.

这篇关于AWS Lambda - 无法导入模块“lambda_function"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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