简体   繁体   English

Python 图层图像失败:“无法导入模块‘lambda_function’:无法从‘PIL’导入名称‘_imaging’”

[英]Python Layer Image Failing: "Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'"

I'm simply trying to be able to use PIL in my Python 3.8 Lambda.我只是想在我的 Python 3.8 Lambda 中使用 PIL。

I'm trying the following steps:我正在尝试以下步骤:

  1. Based on this repo: https://github.com/hidekuma/lambda-layers-for-python-runtime基于这个 repo: 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. Based on this guidance: https://www.splunk.com/en_us/blog/cloud/sharing-code-dependencies-with-aws-lambda-layers.html基于此指南: 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

I then choose my layer in the Lamda configuration, but when I run this code:然后我在 Lamda 配置中选择我的层,但是当我运行这段代码时:

import json
import boto3
import io
from PIL import Image


def lambda_handler(event, context): 
    #etc

...I get the error: ...我收到错误:

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

Where exactly am I going wrong???我到底哪里错了???

Just to clarify, regarding your comment above, Pillow is just a repackaged, updated version of PIL because the original maintainers of PIL stopped working on it a long time ago.澄清一下,关于您上面的评论,Pillow 只是 PIL 的重新打包、更新版本,因为 PIL 的原始维护者很久以前就停止了它的工作。 When you pip install Pillow, you still import it as PIL.当您 pip 安装 Pillow 时,您仍然将其作为 PIL 导入。 In this case they are the same thing.在这种情况下,它们是相同的。

To answer your question, the the Pillow install directions mention:为了回答您的问题,枕头安装说明提到:

Pillow >= 2.1.0 no longer supports import _imaging . Pillow >= 2.1.0 不再支持import _imaging Please use from PIL.Image import core as _imaging instead.请使用from PIL.Image import core as _imaging代替。

I'm not sure where your code imports _imaging , so I think you have a few options:我不确定您的代码在哪里导入_imaging ,所以我认为您有几个选择:

  1. Use an older version of Pillow (pre 2.1.0)使用旧版本的 Pillow (pre 2.1.0)
  2. Find where you are importing _imaging and replace it with the updated from PIL.Image import core as _imaging找到您要导入_imaging的位置并将其替换为from PIL.Image import core as _imaging更新的更新
  3. Update to the current version of Pillow (see my update below)更新到当前版本的 Pillow(请参阅下面的更新)

There is a fourth option which is redirecting the import manually, inspired by this question. 受此问题的启发,还有第四个选项是手动重定向导入。 I would only do this if you can't do one of the first three.如果你不能做前三个之一,我只会这样做。 Put this somewhere in your code that gets run before you do the import that is breaking things.把它放在你的代码中的某个地方,在你执行破坏事情的导入之前运行。 You may have to tweak this slightly to make it work:您可能需要稍微调整一下才能使其正常工作:

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

A later from PIL import _imaging should now really import the new core.稍后from PIL import _imaging现在应该真正导入新内核。

Update:更新:

Updating Pillow may also solve the problem.更新 Pillow 也可以解决问题。 In 7.2.0, I can import _imaging in the old way:在 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'>

I had this issue with an AWS Lambda. Pillow would load for one of my Lambdas, but not another.我在 AWS Lambda 上遇到了这个问题。Pillow 会为我的一个 Lambda 加载,但不会为另一个加载。 I was able to fix it by making sure the Lambda was running on the Architecture "x86_64", not "arm64" in the AWS Lambda Runtime Settings.我能够通过确保 Lambda 在架构“x86_64”上运行而不是 AWS Lambda 运行时设置中的“arm64”来修复它。

I found this answer on the internet :我在网上找到了这个答案:

ImportError: cannot import name _imaging, It can happen if you have PIL installed, then install Pillow on top of it. ImportError: cannot import name _imaging,如果您安装了 PIL,则可能会发生这种情况,然后在其上安装 Pillow。 Go to /usr/local/lib/python2. Go 到 /usr/local/lib/python2。 7/dist-packages/ and delete anything with "PIL" in the name (including directories). 7/dist-packages/ 并删除名称中带有“PIL”的任何内容(包括目录)。 Then re-install Pillow.然后重新安装枕头。 When I run from PIL import Image or from PIL import Image etc, I get the error: ImportError: cannot import name ' imaging' from 'PIL' (C:\Users\Pruthvi\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PIL_ init .py) So odd since these same exact commands were working fine yesterday when I was exploring the PIL and pillow modules.当我从 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 和枕头模块时,这些完全相同的命令运行良好。

I believe that this issue is related to Pillow library.我相信这个问题与枕头库有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法导入模块“lambda_function”: - Unable to import module 'lambda_function': AWS Lambda 层无法导入模块“lambda_function”:没有名为“pyarrow.lib”的模块 - AWS Lambda Layer Unable to import module 'lambda_function': No module named 'pyarrow.lib' AWS Lambda - 无法导入模块“lambda_function” - AWS Lambda - unable to import module 'lambda_function' 无法导入模块“lambda_function”:没有名为“pymongo”的模块 - Unable to import module 'lambda_function': No module named 'pymongo' AWS Lambda Opencv(“无法导入模块‘lambda_function’:libgthread-2.0.so.0:无法打开共享对象文件:没有这样的文件或目录”) - AWS Lambda Opencv ("Unable to import module 'lambda_function': libgthread-2.0.so.0: cannot open shared object file: No such file or directory") 无法导入模块“lambda_function”:没有名为“psycopg2._psycopg aws lambda 函数”的模块 - Unable to import module 'lambda_function': No module named 'psycopg2._psycopg aws lambda function aws lambda 无法导入模块“lambda_function”:没有名为“requests”的模块 - aws lambda Unable to import module 'lambda_function': No module named 'requests' AWS Lambda 导入错误:无法导入模块“lambda_function”:没有名为“confluent_kafka.cimpl”的模块 - AWS Lambda importError: Unable to import module 'lambda_function': No module named 'confluent_kafka.cimpl 无法导入模块“lambda_function”:没有名为“flatten_json”的模块 - Unable to import module 'lambda_function': No module named 'flatten_json' 无法导入模块“lambda_function”:没有名为“aws_xray_sdk”的模块 - Unable to import module 'lambda_function': No module named 'aws_xray_sdk'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM