【问题标题】:Python Layer Image Failing: "Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'"Python层图像失败:“无法导入模块'lambda_function':无法从'PIL'导入名称'_imaging'”
【发布时间】:2020-08-13 17:55:36
【问题描述】:

我只是想在我的 Python 3.8 Lambda 中使用 PIL。

我正在尝试以下步骤:

  1. 基于此回购:https://github.com/hidekuma/lambda-layers-for-python-runtime
cd /mydir 
git clone https://github.com/hidekuma/lambda-layers-for-python-runtime.git 
cd lambda-layers-for-python-runtime 
mkdir dist 
docker-compose up --build
  1. 基于此指南:https://www.splunk.com/en_us/blog/cloud/sharing-code-dependencies-with-aws-lambda-layers.html
aws lambda publish-layer-version --layer-name ImageStorageDependencies
    --description "A Python 3.8 runtime with PIL and boto3 installed." --license-info "MIT" --zip-file fileb://output.zip --compatible-runtimes python3.7 python3.8 --region us-east-2

然后我在 Lamda 配置中选择我的层,但是当我运行此代码时:

import json
import boto3
import io
from PIL import Image


def lambda_handler(event, context): 
    #etc

...我收到错误:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL' 

我到底哪里错了?!?

【问题讨论】:

    标签: python aws-lambda aws-lambda-layers


    【解决方案1】:

    澄清一下,关于您上面的评论,Pillow 只是 PIL 的重新打包、更新版本,因为 PIL 的原始维护者很久以前就停止了它的工作。当你 pip install Pillow 时,你仍然将它作为 PIL 导入。在这种情况下,它们是相同的。

    要回答你的问题,枕头install directions 提及:

    Pillow >= 2.1.0 不再支持import _imaging。请改用from PIL.Image import core as _imaging

    我不确定你的代码在哪里导入_imaging,所以我认为你有几个选择:

    1. 使用旧版本的 Pillow(2.1.0 之前的版本)
    2. 找到您要导入的_imaging 并将其替换为更新后的from PIL.Image import core as _imaging
    3. 更新到当前版本的 Pillow(请参阅下面的更新)

    this 问题的启发,还有第四个选项是手动重定向导入。如果你不能做前三个之一,我只会这样做。把它放在你的代码中的某个地方,在你执行破坏事情的导入之前运行。您可能需要稍微调整一下才能使其正常工作:

    from PIL.Image import core as _imaging
    import sys
    sys.modules['PIL._imaging'] = _imaging
    

    稍后的from PIL import _imaging 现在应该真正导入新内核了。

    更新:

    更新 Pillow 也可以解决问题。在 7.2.0 中,我可以用旧的方式导入 _imaging

    >>> import PIL
    >>> from PIL import _imaging
    >>> print(PIL.__version__)
    7.2.0
    >>> print(_imaging)
    <module 'PIL._imaging' from '...\\lib\\site-packages\\PIL\\_imaging.cp37-win_amd64.pyd'>
    

    【讨论】:

    • 我还没有机会确认这一点,但它肯定是最好的线索。当我有机会测试时,我会报告。感谢您的回答。
    【解决方案2】:

    我在internet找到了这个答案:

    ImportError: cannot import name _imaging,如果你有 安装 PIL,然后在其上安装 Pillow。去 /usr/local/lib/python2. 7/dist-packages/ 并删除任何内容 名称中的“PIL”(包括目录)。然后重新安装枕头。 当我从 PIL 导入图像或从 PIL 导入图像等运行时,我得到 错误:ImportError: cannot import name 'imaging' from 'PIL' (C:\Users\Pruthvi\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PIL_init.py) 太奇怪了,因为这些完全相同的命令昨天运行良好 当我在探索 PIL 和枕头模块时。

    我认为这个问题与 Pillow 库有关。

    【讨论】:

    【解决方案3】:

    我在使用 AWS Lambda 时遇到了这个问题。枕头会加载我的一个 Lambda,但不会加载另一个。我可以通过确保 Lambda 在架构“x86_64”而不是 AWS Lambda 运行时设置中的“arm64”上运行来修复它。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-03-07
    • 2018-11-17
    • 2019-12-03
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多