前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AWS S3 Lambda Python脚本函数执行时报错AttributeError: module ‘PIL‘ has no attribute ‘,Image‘cannot import nam

AWS S3 Lambda Python脚本函数执行时报错AttributeError: module ‘PIL‘ has no attribute ‘,Image‘cannot import nam

作者头像
翎野君
发布2024-04-25 15:00:57
570
发布2024-04-25 15:00:57
举报
文章被收录于专栏:翎野君翎野君

背景

代码示例如下

代码语言:javascript
复制
import PIL
def add_image(self, tag, img, step):
    summary = Summary()
    bio = BytesIO()
 
    if type(img) == str:
        img = PIL.Image.open(img)
    elif type(img) == PIL.Image.Image:
        pass
    else:
        img = scipy.misc.toimage(img)

python脚本在本地可以执行,但是放到S3的Lambda中却总是报这个错

代码语言:javascript
复制
AttributeError: module ‘PIL‘ has no attribute ‘,Image‘cannot import name '_imaging' from 'PIL'

原因

原因是Lambda的Layer层,添加的脚本执行环境eve,打包压缩的zip包有问题,没有按照标准的解压流程去执行。

我一开始是直接把PIL和Pillow包直接压缩打成了一个zip包,这种就少了一些基础的执行环境依赖,标准的打包流程如下。

参考链接

https://github.com/keithrozario/Klayers/issues/154

https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html#packaging-layers-paths

https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html#python-layer-packaging

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 原因
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档