[英]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:您需要执行以下四个步骤:
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。
pip install --target python flatten_json
zip -r layer.zip python
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 上,这应该可以工作,但如果它不起作用,您可以将其替换为当前目录的绝对路径。
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。
flatten_json
as your code to the Lambda.flatten_json
作为 Lambda 的代码。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) 之间做出选择?
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.