简体   繁体   English

AWS Lambda - 无法导入模块“lambda_function”

[英]AWS Lambda - unable to import module 'lambda_function'

Like many others before me, I'm trying to run an AWS Lambda function and when I try to test it, I get像我之前的许多其他人一样,我正在尝试运行 AWS Lambda function,当我尝试测试它时,我得到

"errorMessage": "Unable to import module 'lambda_function'" “errorMessage”:“无法导入模块‘lambda_function’”

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.我的处理程序设置为 lambda_function.lambda_handler,我确实有一个名为 lambda_function.py 的文件,其中包含一个名为 lambda_handler 的 function。 Here's a screenshot as proof:这是一个屏幕截图作为证明: 在此处输入图像描述

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.当我在包含的 IDE 中编写内联代码片段时一切正常,但是当我压缩我的完整程序及其所有依赖项并上传时,我收到了上述错误。

I'm using the Numpy and Scipy packages, which are quite large.我正在使用 Numpy 和 Scipy 包,它们非常大。 My zipped directory is 34 MB, and my unzipped directory 122 MB.我的压缩目录是 34 MB,我的解压目录是 122 MB。 I think this should be fine since the limit is 50 MB for a zipped directory.我认为这应该没问题,因为压缩目录的限制是 50 MB。 It appears to be uploading fine, since I see the message:它似乎上传正常,因为我看到消息:

The deployment package of your Lambda function "one-shot-image-classification" is too large to enable inline code editing.您的 Lambda function “one-shot-image-classification” 的部署 package 太大,无法启用内联代码编辑。 However, you can still invoke your function right now.但是,您现在仍然可以调用您的 function。

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.我已经看到一些帖子通过使用 virtualenv 解决了这个问题,但我不熟悉该技术并且我不确定如何正确使用它。

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.我还看到一些帖子说有时依赖项具有依赖项,我可能需要包含这些依赖项,但我不确定如何找到它。

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:这是 lambda_function.py 的顶部,应该足以看到我正在使用的库,并且我确实有一个 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:这是我正在上传的目录的解压缩版本的屏幕截图: 在此处输入图像描述

I can also post the policy role that my Lambda is using if that could be an issue.如果这可能是个问题,我还可以发布我的 Lambda 正在使用的策略角色。

Any insight is much appreciated!非常感谢任何见解!

UPDATE:更新:

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'这是我尝试过的一种解决方案: 1. git clone https://github.com/Miserlou/lambda-packages 2. 在文档中创建一个名为 new_lambda 的文件夹 3. 将我的 lambda_function.py 和 numpy 文件夹从 lambda-packages 复制到 new_lambda ,以及我根据文章使用 Docker 为 AWS 编译的 scipy 库: https://serverlesscode.com/post/scikitlearn-with-amazon-linux-container/ 4. Zip new_lambda 文件夹,右键单击它,然后选择“压缩”

My results:我的结果:

Unable to import module 'lambda_function': No module named 'lambda_function'无法导入模块“lambda_function”:没有名为“lambda_function”的模块

To reiterate, my file is named lambda_function.py and contains a function called lambda_handler, which accepts two arguments (as seen above).重申一下,我的文件名为 lambda_function.py 并包含一个名为 lambda_handler 的 function,它接受两个 arguments(如上所示)。 This information matches that seen in Handler, also seen above.此信息与在 Handler 中看到的信息相匹配,也在上面看到。

I am using a Mac computer, if that matters.如果重要的话,我正在使用 Mac 电脑。

UPDATE 2更新 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如果我按照上述步骤,而是通过直接选择要压缩的文件然后右键单击并选择“压缩”来代替 zip 文件,我反而会收到错误消息

Unable to import module 'lambda_function': cannot import name 'show_config'无法导入模块“lambda_function”:无法导入名称“show_config”

Also, the precompiled lambda-packages says that they are compiled for "at least Python 2.7", but my lambda runtime is 3.6.此外,预编译的 lambda 包表示它们是为“至少 Python 2.7”编译的,但我的 lambda 运行时是 3.6。 Could this be an issue?这可能是个问题吗?

The problem is that your local numpy and pandas are compiled for the local machine's architecture.问题是你的本地 numpy 和 pandas 是为本地机器的架构编译的。 Since AWS Lambda uses custom Linux, they are probably not compatible.由于 AWS Lambda 使用自定义 Linux,因此它们可能不兼容。

So if you want to use them, you have two choices:所以如果你想使用它们,你有两个选择:

  • Compile dependencies on EC2 instance which uses the same Amazon Linux version as AWS Lambda and create a deployment package.在使用与 AWS Lambda 相同的 Amazon Linux 版本的 EC2 实例上编译依赖项并创建部署包。

  • Use one of the precompiled packages from here使用这里的预编译包之一

PS I've read comments on a post, so I see that the name of the file and function is ok and numpy is giving you trouble. PS 我已经阅读了一篇文章的评论,所以我看到文件和函数的名称没问题,而 numpy 给你带来了麻烦。

解决方案是从这个源压缩 numpy 和 scipy 预编译包。

I had a similar issue:我有一个类似的问题:

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

The fix for me and possibly for you, was to include a blank __init__.py in the same directory as lambda_function.py.对我来说,也可能对你来说,修复是在与 lambda_function.py 相同的目录中包含一个空白的__init__.py

Why does __init__.py fix the issue?为什么__init__.py解决这个问题?

I understand that it is needed for the directory (lib in my case, . in yours) to be considered a valid Python package.我知道目录(在我的情况下为 lib,在您的情况下为 .)需要被视为有效的 Python 包。

Here is the reference doc I based that hypothesis on: 5.2.1.这是我基于该假设的参考文档: 5.2.1。 Regular packages - Python 3.7.3 documentation 常规包 - Python 3.7.3 文档

I ran into this issue on MacOS as well.我在 MacOS 上也遇到了这个问题。 I see you mention the way you select your files affected whether it worked properly.我看到你提到你选择文件的方式会影响它是否正常工作。 Turns out this is true!事实证明这是真的!

On Mac, if your .DS_Store/MacOS hidden folder sneaks into the directory it seems to break Lambda!在 Mac 上,如果您的 .DS_Store/MacOS 隐藏文件夹潜入该目录,它似乎会破坏 Lambda!

The solution is to解决办法是

rm -rf .DS_Store

In the deployment zip folder.在部署 zip 文件夹中。

I was receiving this same error, but the cause of the error was different.我收到了同样的错误,但错误的原因不同。 Adding an answer here in case some other weary StackOverflow wanderer finds it helpful.在这里添加一个答案,以防其他一些厌倦的 StackOverflow 流浪者发现它有帮助。

In my case, I was attempting to upload the below directory where package is a sample python package dependency and my function code is enclosed in lambda_function.py:就我而言,我试图上传以下目录,其中 package 是一个示例 python 包依赖项,我的函数代码包含在 lambda_function.py 中: 在此处输入图片说明

I was zipping the entire function directory, which was resulting in the following file structure when deployed to lambda:我正在压缩整个function目录,这在部署到 lambda 时产生了以下文件结构: 在此处输入图片说明

In order to run properly, both lambda_function.py and the package directory should be in the top-level lambda directory.为了正常运行, lambda_function.pypackage目录都应该在顶级 lambda 目录中。 In my case ConfigureAppFlow .在我的情况下ConfigureAppFlow The function directory is an extra layer that is causing the error. function目录是导致错误的额外层。

To fix this, instead of compressing my function directory, I directly compressed the two items inside:为了解决这个问题,我没有压缩我的function目录,而是直接压缩了里面的两个项目: 在此处输入图片说明

This resulted in the following file structure when deployed to lambda (ignore the _MACOSX folder):部署到 lambda 时,这会导致以下文件结构(忽略 _MACOSX 文件夹): 在此处输入图片说明

In summary, I'm sure there are a ton of different causes of this issue, but the first thing to check is that the zip file you are uploading to lambda correctly results in a file structure that places lambda_function.py and any dependent packages in your top-level lambda directory.总之,我确定此问题有很多不同的原因,但首先要检查的是,您上传到 lambda 的 zip 文件是否正确生成了将lambda_function.py和任何依赖包放入的文件结构您的顶级 lambda 目录。

The way compressing files works seems to be the issue here, I just solved the same problem with my code files.压缩文件的工作方式似乎是这里的问题,我刚刚用我的代码文件解决了同样的问题。 Simply highlight the files/folders you want to compress and right-click and compress that.只需突出显示要压缩的文件/文件夹,然后右键单击并压缩它。

For whatever reason, I keep getting an additional folder inside my initial project folder which throw off AWS as it doesn't know to go 2 directories deep but only looks at the root directory for our files.无论出于何种原因,我一直在我的初始项目文件夹中获得一个额外的文件夹,它会抛出 AWS,因为它不知道 go 2 个目录的深度,但只查看我们文件的根目录。

暂无
暂无

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

相关问题 无法导入模块“lambda_function”: - Unable to import module 'lambda_function': 无法导入模块“lambda_function”:没有名为“psycopg2._psycopg aws lambda 函数”的模块 - Unable to import module 'lambda_function': No module named 'psycopg2._psycopg aws lambda function aws lambda 无法导入模块“lambda_function”:没有名为“requests”的模块 - aws lambda Unable to import module 'lambda_function': No module named 'requests' AWS Lambda 导入错误:无法导入模块“lambda_function”:没有名为“confluent_kafka.cimpl”的模块 - AWS Lambda importError: Unable to import module 'lambda_function': No module named 'confluent_kafka.cimpl AWS Lambda 层无法导入模块“lambda_function”:没有名为“pyarrow.lib”的模块 - AWS Lambda Layer Unable to import module 'lambda_function': No module named 'pyarrow.lib' 无法导入模块“lambda_function”:没有名为“aws_xray_sdk”的模块 - Unable to import module 'lambda_function': No module named 'aws_xray_sdk' 无法导入模块“lambda_function”:没有名为“pymongo”的模块 - Unable to import module 'lambda_function': No module named 'pymongo' AWS Lambda Opencv(“无法导入模块‘lambda_function’:libgthread-2.0.so.0:无法打开共享对象文件:没有这样的文件或目录”) - AWS Lambda Opencv ("Unable to import module 'lambda_function': libgthread-2.0.so.0: cannot open shared object file: No such file or directory") 无法导入模块“lambda_function”:没有名为“flatten_json”的模块 - Unable to import module 'lambda_function': No module named 'flatten_json' Python 图层图像失败:“无法导入模块‘lambda_function’:无法从‘PIL’导入名称‘_imaging’” - Python Layer Image Failing: "Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM