[英]How do I import a Python lambda layer?
I have a file with this as the contents.我有一个以此为内容的文件。
def print_hello_world():
print ('Hello World')
It's zipped up in a folder with a __init__.py
file.它被压缩在一个包含
__init__.py
文件的文件夹中。
I add this as the layer and set the correct runtime of python3.6.我将其添加为层并设置了 python3.6 的正确运行时间。
How do import it into my lambda function via lambda code?如何通过 lambda 代码将其导入我的 lambda function?
Edit: After researching I found that lambda mounts the layers at /opt and if you add /opt to your syspath via sys.path.insert(0, '/opt') then you can import your layers.编辑:经过研究,我发现 lambda 在 /opt 上安装层,如果您通过 sys.path.insert(0, '/opt') 添加 /opt 到您的系统路径,那么您可以导入您的层。
You can see your layers at: print(os.listdir("/opt"))您可以在以下位置查看图层:print(os.listdir("/opt"))
There's gotta be a more elegant way to do this!必须有一种更优雅的方法来做到这一点!
So I've recently ran into this issue, and I believe I found a cleaner way to import your layers.所以我最近遇到了这个问题,我相信我找到了一种更简洁的方法来导入你的图层。
First for the structure of the zip file which you upload:首先是您上传的 zip 文件的结构:
__init__.py
file__init__.py
文件python
python
import {filename}
import {filename}
So if your script in the python
folder was called something like custom_helper.py
, import it in your lambda with import custom_helper
.因此,如果您在
python
文件夹中的脚本被称为custom_helper.py
,请使用import custom_helper
将其导入到您的 lambda 中。
I am not sure if this is the clean way to do it, but it seems simple enough to start.我不确定这是否是一种干净的方法,但它似乎很容易开始。
Your zip file should have the following structure:您的 zip 文件应具有以下结构:
python/lib/python3.7/site-packages python/lib/python3.7/site-packages
That is, it needs a folder named Python, and within that a folder named lib, and within that a folder named python3.7, and within that a folder named site-packages.也就是说,它需要一个名为 Python 的文件夹,在该文件夹内有一个名为 lib 的文件夹,在该文件夹内有一个名为 python3.7 的文件夹,在该文件夹内有一个名为 site-packages 的文件夹。 Anything inside that folder will be available for import.
该文件夹中的任何内容都可以导入。
(If you're using another version of Python, that version should be in the path instead of 3.7) (如果您使用的是其他版本的 Python,该版本应该在路径中而不是 3.7)
You'll have to:你必须:
sudo docker run -v "$PWD":/var/task "lambci/lambda:build-${penv}" /bin/sh -c "pip install -r requirements.txt -t python/lib/${penv}/site-packages/; exit"
sudo docker run -v "$PWD":/var/task "lambci/lambda:build-${penv}" /bin/sh -c "pip install -r requirements.txt -t python/lib/${penv}/site-packages/; exit"
sys.path
in your Lambda before importsys.path
import sys;
sys.path.append('/opt/python/lib/python3.7/site-packages');
import apsw
print(apsw.__file__)
/opt/python/lib/python3.7/site-packages/apsw.cpython-37m-x86_64-linux-gnu.so
Update: It looks like you need to place build under /opt/python so you can use it without sys.path.append('/opt/python/lib/python3.7/site-packages');
更新:看起来您需要将构建放在 /opt/python 下,以便您可以在没有
sys.path.append('/opt/python/lib/python3.7/site-packages');
情况下使用它sys.path.append('/opt/python/lib/python3.7/site-packages');
mkdir -vp my-layer/python && cd my-layer/python
python3 -m pip install click -t ./
cd ..
zip -r9 my-layer python
now add layer to lambda function and you can import:现在将层添加到 lambda 函数,您可以导入:
import click
SOLUTION解决方案
You must to allocate all the packages you install with pip install... into a zip file that ends with the folllowing file structure:您必须将使用 pip install... 安装的所有软件包分配到以以下文件结构结尾的 zip 文件中:
[your zip file].zip
|_python/
|_all.../
|_your.../
|_packages, modules.../
Another boring way is:另一种无聊的方式是:
[your zip file].zip
|_python/
|_lib/
|_python3.x/
|_site-packages/
|_all.../
|_your.../
|_packages, modules.../
with this structure you lambda code get the modules you have.使用此结构,您 lambda 代码将获得您拥有的模块。
I hope this make done your issue.我希望这可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.