Python图层图像失败:"Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'"
Python Layer Image Failing: "Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL'"
我只是想在我的 Python 3.8 Lambda 中使用 PIL。
我正在尝试以下步骤:
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
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'
我到底哪里错了?!?
我在 internet 上找到了这个答案:
ImportError: cannot import name _imaging, It can happen if you have
PIL installed, then install Pillow on top of it. Go to
/usr/local/lib/python2. 7/dist-packages/ and delete anything with
"PIL" in the name (including directories). 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.
我认为这个问题与 Pillow 库有关。
澄清一下,关于你上面的评论,Pillow 只是 PIL 的重新打包、更新版本,因为 PIL 的原始维护者很久以前就停止了对它的工作。当你 pip install Pillow 时,你仍然将它作为 PIL 导入。在这种情况下,它们是同一件事。
为了回答你的问题,枕头 install directions 提及:
Pillow >= 2.1.0 no longer supports import _imaging
. Please use from PIL.Image import core as _imaging
instead.
我不确定你的代码在哪里导入 _imaging
,所以我认为你有几个选择:
- 使用旧版本的 Pillow(2.1.0 之前)
- 找到您要导入的位置
_imaging
并将其替换为更新后的 from PIL.Image import core as _imaging
- 更新到当前版本的 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'>
我在使用 AWS Lambda 时遇到了这个问题。 Pillow 会为我的一个 Lambda 加载,但不会为另一个加载。我能够通过确保 Lambda 在架构“x86_64”中是 运行,而不是 AWS Lambda 运行时设置中的“arm64”来修复它。
我只是想在我的 Python 3.8 Lambda 中使用 PIL。
我正在尝试以下步骤:
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
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'
我到底哪里错了?!?
我在 internet 上找到了这个答案:
ImportError: cannot import name _imaging, It can happen if you have PIL installed, then install Pillow on top of it. Go to /usr/local/lib/python2. 7/dist-packages/ and delete anything with "PIL" in the name (including directories). 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.
我认为这个问题与 Pillow 库有关。
澄清一下,关于你上面的评论,Pillow 只是 PIL 的重新打包、更新版本,因为 PIL 的原始维护者很久以前就停止了对它的工作。当你 pip install Pillow 时,你仍然将它作为 PIL 导入。在这种情况下,它们是同一件事。
为了回答你的问题,枕头 install directions 提及:
Pillow >= 2.1.0 no longer supports
import _imaging
. Please usefrom PIL.Image import core as _imaging
instead.
我不确定你的代码在哪里导入 _imaging
,所以我认为你有几个选择:
- 使用旧版本的 Pillow(2.1.0 之前)
- 找到您要导入的位置
_imaging
并将其替换为更新后的from PIL.Image import core as _imaging
- 更新到当前版本的 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'>
我在使用 AWS Lambda 时遇到了这个问题。 Pillow 会为我的一个 Lambda 加载,但不会为另一个加载。我能够通过确保 Lambda 在架构“x86_64”中是 运行,而不是 AWS Lambda 运行时设置中的“arm64”来修复它。