在尝试在AWS Lambda中使用Pillow时,出现了“无法导入名称'_imaging'”的错误。

4
我正在尝试创建一个使用PIL的AWS Lambda函数。因此,我在项目目录中安装了PIL:
pip install Pillow -t .

第一次在本地机器上运行时,它导致了

ImportError: cannot import name '_imaging'

我以为我做错了什么,于是从项目目录中删除了PIPPillow*目录,并重新安装了PIL。然后它就正常工作了。

不幸的是,当我将所有内容打包成ZIP并发布到AWS时,该函数再次出现故障。

Traceback (most recent call last):
  File "/var/task/myfile.py", line 9, in lambda_handler
    from PIL import Image
  File "/var/task/PIL/Image.py", line 64, in <module>
    from . import _imaging as core
ImportError: cannot import name '_imaging'

这个错误的确切原因是什么,如何修复它?
以下是我项目目录中的文件列表:
$ ls
bin                          chardet-3.0.4.dist-info  Pillow-5.2.0.dist-info     ThumbnailEnergent_Lambda.zip
certifi                      idna                     requests                   myfile.py
certifi-2018.4.16.dist-info  idna-2.7.dist-info       requests-2.19.1.dist-info  urllib3
chardet                      PIL                      tests                      urllib3-1.23.dist-info

1
你找到答案了吗? - Jack
嘿,我在这里发布了一个解决方案,它不需要Docker。你只需要创建一个层,关键是你本地有正确版本的Python,如果需要可以安装。https://dev59.com/XFcO5IYBdhLWcg3wpTKW#74736780 - MattC
1个回答

4
我今天也遇到了这个问题,后来发现我的Lambda运行时语言是Python3.6,因为我正在使用从早期项目中复制的Cloudformation模板。问题在于我已经下载了Pillow使用的Python 3.7的zip文件,而Pillow将其_imaging C模块文件与Python运行时命名。
为了解决这个问题,我只需要将运行时语言从Python 3.6更改为Python 3.7。所以请确保上传脚本和Lambda函数的Python版本完全相同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接