简体   繁体   English

无法导入模块“lambda_function”:没有名为“flatten_json”的模块

[英]Unable to import module 'lambda_function': No module named 'flatten_json'

Gettting the below error while running the lambda code, I am using the library called运行 lambda 代码时出现以下错误,我正在使用名为的库

from flatten_json import flatten

I tried to look for a lambda layer, but did not find any online, please let me know if any one used this before or suggest any alternative我试图寻找一个 lambda 层,但没有在网上找到任何层,请让我知道是否有人以前使用过它或建议任何替代方案

flatten_json library is missing.缺少 flatten_json 库。

Use pip install flatten_json to get it使用pip install flatten_json获取

There are four steps you need to do:您需要执行以下四个步骤:

  1. Download the dependency.下载依赖。
  2. Package it in a ZIP file. Package 在 ZIP 文件中。
  3. Create a new layer in AWS . 在 AWS 中创建一个新层
  4. Associate the layer with your Lambda . 将图层与您的 Lambda 关联

My answer will focus on 1. and 2. as they are what is most important to your problem.我的回答将集中在 1. 和 2. 上,因为它们对您的问题最重要。 Unfortunately, packaging Python dependencies can be a bit more complicated than for other runtimes.不幸的是,打包 Python 依赖项可能比其他运行时复杂一些。

The main issue is that some dependencies use C code under the hood, especially performance critical libraries, for example for Machine Learning etc.主要问题是某些依赖项在后台使用 C 代码,尤其是性能关键库,例如机器学习等。

C code needs to be compiled and if you run pip install on your machine the code will be compiled for your computer. C 代码需要编译,如果您在您的机器上运行pip install ,代码将为您的计算机编译。 AWS Lambdas use a linux kernel and amd64 architecture. AWS Lambda 使用linux kernel 和amd64架构。 So if you are running pip install on a Linux machine with AMD or Intel processor, you can indeed just use pip install .因此,如果您在装有 AMD 或 Intel 处理器的 Linux 机器上运行pip install ,您确实可以只使用pip install But if you use macOS or Windows, your best bet is Docker.但如果你使用 macOS 或 Windows,你最好的选择是 Docker。

Without Docker不带 Docker

pip install --target python flatten_json
zip -r layer.zip python

With Docker带Docker

The lambci project provides great Docker container for building and running Lambdas. lambci 项目为构建和运行 Lambdas 提供了出色的 Docker 容器。 In the following example I am using their build-python3.8 image.在以下示例中,我使用了他们的build-python3.8映像。

docker run --rm -v $(pwd):/var/task lambci/lambda:build-python3.8 pip install --target python flatten_json
zip -r layer.zip python

Be aware that $(pwd) is meant to be your current directoy.请注意, $(pwd)是您当前的目录。 On macOS and WSL this should work, but if it does not work you can just replace it with the absolute path to your current directory.在 macOS 和 WSL 上,这应该可以工作,但如果它不起作用,您可以将其替换为当前目录的绝对路径。

Explanation解释

Those commands will install the dependency into a target folder called python .这些命令会将依赖项安装到名为python目标文件夹中。 The name is important, because it is one of two folders of a layer where Lambda looks for dependencies .名称很重要,因为它是Lambda 查找依赖项的层的两个文件夹之一

The python folder is than archived recursively ( -r ) in a file called layer.zip . python文件夹在名为layer.zip的文件中递归( -r )存档。

Your next step is to create a new Layer in AWS and associated your function with that layer.您的下一步是在 AWS 中创建一个新层,并将您的 function 与该层相关联。

There are two options to choose from有两个选项可供选择

Option 1) You can use a deployment package to deploy your function code to Lambda.选项 1)您可以使用部署 package 将 function 代码部署到 Lambda。

  • The deployment package (For eg zip) will contain your function's code and any dependencies used to run the function's code.部署 package(例如 zip)将包含您的函数代码和用于运行函数代码的任何依赖项。
  • Hence, you can package flatten_json as your code to the Lambda.因此,您可以将 package flatten_json作为 Lambda 的代码。
  • Check Creating a function with runtime dependencies page in aws documentation, it explains the use-case of having requests library.检查在 aws 文档中创建具有运行时依赖项页面的 function ,它解释了具有请求库的用例。 In your scenario, the library would be flatten_json在您的场景中,该库将是flatten_json

Option 2) Create a layer that has the library dependencies you need, in your case just flatten_json .选项 2)创建一个具有您需要的库依赖项的层,在您的情况下只是flatten_json And then attach that layer to your Lambda.然后将该层附加到您的 Lambda。

How to decide between 1) and 2)?如何在 1) 和 2) 之间做出选择?

  • Use Option 1) when you just need the dependencies in just that one Lambda.当您只需要一个 Lambda 中的依赖项时,请使用选项 1) No need to create an extra step of creating a layer.无需创建创建图层的额外步骤。
  • Layers are useful if you have some common code that you want to share across different Lambdas.如果您有一些想要在不同 Lambda 之间共享的通用代码,层会很有用。 So if you need the library accessible in other Lambdas as well, then it's good to have a layer[ Option 2) ] that can be attached to different lambdas.因此,如果您还需要其他 Lambda 中可访问的库,那么最好有一个可以附加到不同 lambda 的层[选项 2) ]。

You can do this is in a Lambda if you don´t want to create the layer.如果您不想创建图层,可以在 Lambda 中执行此操作。 Keep in mind it will run slower since it has to install the library in every run:请记住,它会运行得更慢,因为它必须在每次运行时安装库:

import sys
import subprocess
subprocess.call('pip install flatten_json -t /tmp/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
sys.path.insert(1, '/tmp/')
import flatten_json

暂无
暂无

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

相关问题 无法导入模块“lambda_function”:没有名为“pymongo”的模块 - Unable to import module 'lambda_function': No module named 'pymongo' 无法导入模块“lambda_function”:没有名为“psycopg2._psycopg aws lambda 函数”的模块 - Unable to import module 'lambda_function': No module named 'psycopg2._psycopg aws lambda function 无法导入模块“lambda_function”: - Unable to import module 'lambda_function': 无法导入模块“lambda_function”:没有名为“aws_xray_sdk”的模块 - Unable to import module 'lambda_function': No module named 'aws_xray_sdk' 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' AWS Lambda - 无法导入模块“lambda_function” - AWS Lambda - unable to import module 'lambda_function' Python 图层图像失败:“无法导入模块‘lambda_function’:无法从‘PIL’导入名称‘_imaging’” - Python Layer Image Failing: "Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'" 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")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM