Python 导入错误:无法导入名称 _imaging
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25340698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: cannot import name _imaging
提问by tomooka
I installed Pillow, and after I want to do:
我安装了Pillow,然后我想做:
from PIL import Image
I get the following error:
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 61, in <module>
ImportError: cannot import name _imaging
However, if I import these separately, everything is fine, ie:
但是,如果我分别导入这些,一切都很好,即:
import _imaging
import Image
Do you know what the problem might be?
你知道可能是什么问题吗?
采纳答案by cbeytas
I ran into this problem as well. It can happen if you have PIL installed, then install Pillow on top of it.
我也遇到了这个问题。如果你安装了 PIL,然后在它上面安装 Pillow,就会发生这种情况。
Go to /usr/local/lib/python2.7/dist-packages/ and delete anything with "PIL" in the name (including directories). If the Pillow .egg file is there you might as well delete that too. Then re-install Pillow.
转到 /usr/local/lib/python2.7/dist-packages/ 并删除名称中带有“PIL”的任何内容(包括目录)。如果 Pillow .egg 文件在那里,您也可以将其删除。然后重新安装枕头。
substitute "python2.7" for the version of python you're using.
将“python2.7”替换为您正在使用的 python 版本。
回答by James Paul Shaulis
For pillow to work PIL must be in /usr/local/lib/python2.7 or3/dist-packages/PIL.py.
要使枕头工作,PIL 必须在 /usr/local/lib/python2.7 或 3/dist-packages/PIL.py 中。
In dist-packages PIL.py should have a folder.
在 dist-packages 中 PIL.py 应该有一个文件夹。
1) sudo apt-get update 2) pip install Pillow
1) sudo apt-get update 2) pip install Pillow
PIL != PiL, have a nice day.
PIL != PiL,祝你有美好的一天。
回答by joarleymoraes
This also happens if you built Pillowin one OS and then copied the contents of site-packagesto another one. For example, if you are creating AWS Lambda deployment package, that's the error you will face when running the Lambda function. If that's the case, then Pillow needs to be installed in a Amazon Linux instance and you have to use the resulting site-packagesin your deployment package. See instructions and details here:
如果您内置Pillow一个操作系统,然后将其内容复制site-packages到另一个操作系统,也会发生这种情况。例如,如果您正在创建 AWS Lambda 部署包,这就是您在运行 Lambda 函数时将面临的错误。如果是这种情况,则需要将 Pillow 安装在 Amazon Linux 实例中,并且您必须使用生成site-packages的部署包。在此处查看说明和详细信息:
http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html
http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html
回答by user9521248
What is your version of pillow?
你的枕头是什么版本?


Pillow >= 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaginginstead. Link tot he official document
Pillow >= 2.1.0 不再支持import _imaging. 请from PIL.Image import core as _imaging改用。 官方文档链接
回答by Benyamin Jafari
I had the same problem so I solved that by updating this package using the command below:
我遇到了同样的问题,所以我通过使用以下命令更新这个包来解决这个问题:
pip install -U Pillow
回答by ASSILI Taher
I'm using Flask with Google App Engine. I have the module Pillow installed via this command:
我正在将 Flask 与 Google App Engine 一起使用。我通过以下命令安装了 Pillow 模块:
pip install -t lib pillow
I fixed this error by defined PIL in my app.yaml file:
我通过在 app.yaml 文件中定义的 PIL 修复了这个错误:
libraries:
- name: PIL
version: latest
回答by Moriuks
I had the same problem when it tried to deploy a lambda package, the thing is that you have to precompile the package emulating the lambda architecture/runtime that you are going to use, otherwise you'll get cannot import name _imaging. 2 ways of solving this:
我在尝试部署 lambda 包时遇到了同样的问题,问题是您必须预编译该包来模拟您将要使用的 lambda 架构/运行时,否则您将获得cannot import name _imaging. 解决这个问题的2种方法:
1 - spin up an EC2 Amazon Linux instance.( i will only cover this part)
1 - 启动一个EC2 Amazon Linux 实例。(我只会介绍这部分)
2 - Use dockers.
2 - 使用泊坞窗。
Short solution
简短的解决方案
- Install Python 3 in Amazon Linux 2 intance. (Must be python3.X you plan to use in lambda)
- Install a virtual environment under the ec2-user home directory.
- Activate the environment, and then install Boto 3.
- Install Pillow
- Create a ZIP archive with the contents of the library(PIL and Pillow.libs)
- Add your function code to the archive.
- Update your the lambda.(AWS CLI)
- 在 Amazon Linux 2 实例中安装 Python 3。(必须是你打算在 lambda 中使用的 python3.X)
- 在 ec2-user 主目录下安装虚拟环境。
- 激活环境,然后安装Boto 3。
- 安装枕头
- 创建包含库内容的 ZIP 存档(PIL 和 Pillow.libs)
- 将您的功能代码添加到存档中。
- 更新您的 lambda。(AWS CLI)
Long solution
长解
If Python 3 isn't already installed, then install the package using the yum package manager.
$ sudo yum install python3 -yCreate a virtual environment under the ec2-user home directory
The following command creates the app directory with the virtual environment inside of it. You can change my_app to another name. If you change my_app, make sure that you reference the new name in the remaining resolution steps.
$ python3 -m venv my_app/envActivate the virtual environment and install Boto 3
Attach an AWS Identity and Access Management (IAM) role to your EC2 instance with the proper permissions policies so that Boto 3 can interact with the AWS APIs. For other authentication methods....For a quick use you can set your credential using
$ aws confifuresee documentation( you will need this in step 7)
如果尚未安装 Python 3,请使用 yum 包管理器安装该包。
$ sudo yum install python3 -y在 ec2-user 家目录下创建虚拟环境
以下命令创建应用程序目录,其中包含虚拟环境。您可以将 my_app 更改为其他名称。如果更改 my_app,请确保在其余解析步骤中引用新名称。
$ python3 -m venv my_app/env激活虚拟环境并安装 Boto 3
使用适当的权限策略将 AWS Identity and Access Management (IAM) 角色附加到您的 EC2 实例,以便 Boto 3 可以与 AWS API 交互。对于其他身份验证方法......为了快速使用,您可以使用
$ aws confifure查看文档设置您的凭证(您将在第 7 步中需要它)
3.1 Activate the environment by sourcing the activate file in the bin directory under your project directory.
3.1 通过在项目目录下的 bin 目录中获取 activate 文件来激活环境。
`$ source ~/my_app/env/bin/activate`
3.2. Make sure that you have the latest pip module installed within your environment.
$ pip install pip --upgrade
3.2. 确保您的环境中安装了最新的 pip 模块。
$ pip install pip --upgrade
3.3 Use the pip command to install the Boto 3 library within our virtual environment.
3.3 使用 pip 命令在我们的虚拟环境中安装 Boto 3 库。
`pip install boto3`
Install libraries with pip.
$ pip install Pillow
使用 pip 安装库。
$ pip install Pillow
4.1 Deactivate the virtual environment.
4.1 停用虚拟环境。
`$ deactivate`
Create a ZIP archive with the contents of the library.
change directory to where pip is installes. it should be something like /my_app/env/lib/python3.x/site-packages.
使用库的内容创建 ZIP 存档。
将目录更改为安装 pip 的位置。它应该类似于 /my_app/env/lib/python3.x/site-packages。
IMPORTANT: the key here is to zip the file inside site-packages into your lambda.(i only used PIL and Pillow.libs to save space but you can zip everything)
重要提示:这里的关键是将站点包内的文件压缩到您的 lambda 中。(我只使用 PIL 和 Pillow.libs 来节省空间,但您可以压缩所有内容)
5.1 ZIP everything thats inside the PIL folder.
5.1 压缩 PIL 文件夹中的所有内容。
`zip -r9 PIL.zip ./PIL/`
add the Pillow.libs to your ZIP
`zip -gr PIL.zip Pillow.libs`
Add your function code to the archive. you can do this in the console if it just on file of code, but i recomend doing it in this step.If you don't have your code,just create a file using vi or nano and save it with the name that your lambda handler will use (in this case will use lambda_function.py).
zip -g PIL.zip lambda_function.pyUpdate your the lambda.(AWS CLI) if you haven't create a lambda function,do it now before updating the function from the aws cli, make sure that you have the right permission to update lambda from the aws cli.
change LAMBDAFUNCTIONNAME for your function name
aws lambda update-function-code --function-name LAMBDAFUNCTIONNAME P --zip-file fileb://PIL.zip
将您的功能代码添加到存档中。如果只是在代码文件中,您可以在控制台中执行此操作,但我建议在此步骤中执行此操作。如果您没有代码,只需使用 vi 或 nano 创建一个文件,并使用您的 lambda 名称保存它处理程序将使用(在这种情况下将使用 lambda_function.py)。
zip -g PIL.zip lambda_function.py更新您的 lambda。(AWS CLI)如果您尚未创建 lambda 函数,请在从 aws cli 更新函数之前立即执行,确保您具有从 aws cli 更新 lambda 的正确权限。
将 LAMBDAFUNCTIONNAME 更改为您的函数名称
aws lambda update-function-code --function-name LAMBDAFUNCTIONNAME P --zip-file fileb://PIL.zip
Getting out of the first loop of hell
走出地狱的第一个循环
go to your lambda console and test your code, make sure you use the same runtime/python version you used in the EC2 instance

