Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

amazon-web-services - AWS lambda无法导入模块“ lambda_function”:没有名为PIL的模块(AWS lambda Unable to import module 'lambda_function': No module named PIL)

I am using a lambda function of SearchFacesbyimage And I am using this doc https://aws.amazon.com/blogs/machine-learning/build-your-own-face-recognition-service-using-amazon-rekognition/

(我正在使用SearchFacesbyimage的lambda函数,并且正在使用此文档https://aws.amazon.com/blogs/machine-learning/build-your-own-face-recognition-service-using-amazon-rekognition/)

where for comparison I am using this

(我在哪里比较)

from PIL import Image

And I am getting this error Unable to import module 'lambda_function': No module named PIL

(而且我收到此错误Unable to import module 'lambda_function': No module named PIL)

  ask by ephemeral translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are getting this error as PIL for Python 2.x or PILLOW for 3.x are not standard libraries available in python lambda environment.

(之所以会出现此错误,是因为Python 2.x的PIL或3.x的PILLOW在python lambda环境中不是标准库。)

To use such a library , you have to make a custom deployment package of all libraries you need as well as the python code you want to deploy.

(要使用这样的库,您必须为所有需要的库以及要部署的python代码制作一个自定义部署包。)

This package can be made easily either in docker or by using EC2 instance .

(可以在docker中或使用EC2实例轻松制作此软件包。)

here is the procedure how you will make that deployment package on EC2 :

(这是在EC2上制作该部署程序包的过程:)

  1. Suppose you have your file named CreateThumbnail.py

    (假设您的文件名为CreateThumbnail.py)

  2. If your source code is on a local host, copy it over to EC2.

    (如果您的源代码在本地主机上,请将其复制到EC2。)

    scp -i key.pem /path/to/my_code.py ec2-user@public-ip-address:~/CreateThumbnail.py

    (scp -i key.pem /path/to/my_code.py ec2-user @ public-ip-address:?/ CreateThumbnail.py)

  3. Connect to a 64-bit Amazon Linux instance via SSH.

    (通过SSH连接到64位Amazon Linux实例。)

    ssh -i key.pem ec2-user@public-ip-address

    (ssh -i key.pem ec2-user @ public-ip-address)

  4. Install Python 3.6 and virtualenv using the following steps:

    (使用以下步骤安装Python 3.6和virtualenv:)

    a) sudo yum install -y gcc zlib zlib-devel openssl openssl-devel

    (a) sudo yum install -y gcc zlib zlib-devel openssl openssl-devel)

    b) wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

    (b) wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz)

    c) tar -xzvf Python-3.6.1.tgz

    (c) tar -xzvf Python-3.6.1.tgz)

    d) cd Python-3.6.1 && ./configure && make

    (d) cd Python-3.6.1 && ./configure && make)

    e) sudo make install f sudo /usr/local/bin/pip3 install virtualenv

    (e) sudo make install f sudo / usr / local / bin / pip3安装virtualenv)

  5. Choose the virtual environment that was installed via pip3

    (选择通过pip3安装的虚拟环境)

    /usr/local/bin/virtualenv ~/shrink_venv

    (/ usr / local / bin / virtualenv?/ shrink_venv)

    source ~/shrink_venv/bin/activate

    (源?/ shrink_venv / bin / activate)

  6. Install libraries in the virtual environment

    (在虚拟环境中安装库)

    pip install Pillow

    (点安装枕头)

    pip install boto3

    (点安装boto3)

  7. Add the contents of lib and lib64 site-packages to your .zip file.

    (将lib和lib64站点软件包的内容添加到您的.zip文件中。)

    Note that the following steps assume you used Python runtime version 3.6.

    (请注意,以下步骤假定您使用的是Python运行时版本3.6。)

    If you used version 2.7 you will need to update accordingly.

    (如果您使用的是2.7版,则需要进行相应的更新。)

    cd $VIRTUAL_ENV/lib/python3.6/site-packages

    (cd $ VIRTUAL_ENV / lib / python3.6 / site-packages)

    zip -r9 ~/CreateThumbnail.zip

    (zip -r9?/ CreateThumbnail.zip)

    note- To include all hidden files, use the following option:

    (注意-要包括所有隐藏文件,请使用以下选项:)

    zip -r9 ~/CreateThumbnail.zip

    (zip -r9?/ CreateThumbnail.zip)

  8. Add your python code to the .zip file

    (将您的python代码添加到.zip文件)

    cd ~

    (光盘?)

    zip -g CreateThumbnail.zip CreateThumbnail.py

    (zip -g CreateThumbnail.zip CreateThumbnail.py)

Now CreateThumbnail.zip is your custom deployment package , just copy it to s3 and upload it to your lambda.

(现在, CreateThumbnail.zip是您的自定义部署程序包,只需将其复制到s3并将其上传到您的lambda。)

This example is taken from official AWS documentation at https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html

(该示例摘自AWS官方文档, 网址https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...