ImportError: cannot import name _imaging
Asked Answered
V

17

92

I installed Pillow, and after I want to do:

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?

Varion answered 16/8, 2014 at 13:37 Comment(1)
If using AWS Lambda, I posted a solution here that does not require Docker. You just create a layer, the trick being that you have the correct version of Python locally, which you can install if needed. https://mcmap.net/q/242213/-getting-pil-pillow-4-2-1-to-upload-properly-to-aws-lambda-py3-6Jacobs
F
43

I ran into this problem as well. 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). If the Pillow .egg file is there you might as well delete that too. Then re-install Pillow.

substitute "python2.7" for the version of python you're using.

Frustrated answered 14/10, 2014 at 21:17 Comment(5)
On my Mac I had to do this under /usr/local/lib/python2.7/site-packages Fixed, thanks.Promotive
On Ubuntu, I ran sudo pip3 uninstall uninstall pip3 4 times (because there was always still something to remove) until there was nothing left to remove. And then running pip3 install -U pillow --user, no more errors from Pillow.Strouse
thanks! pip uninstall pillow pip install pillow was enoughParamedical
Thanks you led me to the solution. In Ubuntu 16.04. I had problems running in Python3. It was enough for me to remove the "official" package python3-pil (i.e., sudo apt-get remove python3-pil), because I had installed Pillow via pipPettaway
Unistall and reinstall of pillow worked perfectly. pip uninstall pillow pip install -U pillowKessia
A
103

I had the same problem and I solved that by upgrading this package using the command below:

pip install -U Pillow
Admass answered 2/4, 2019 at 10:44 Comment(0)
S
58

This also happens if you built Pillow in one OS and then copied the contents of site-packages to 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-packages in your deployment package. See instructions and details here:

http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html

Schoolbook answered 14/10, 2017 at 13:42 Comment(7)
I use CodeBuild in combination with Elastic Container Repository. ECR is used to store the Docker image used by CodeBuild. I was using an image based on the python:3.5.2 image and I had the error message. Once I switched to amazonlinux:latest and installed Python manually in it, the issue was fixed. Thanks a lot for your insight!Ouse
Glad it helped :)Schoolbook
Does this mean that i can't create the zip file with proper contents in it on my Mac?Schaeffer
This solution won't work for me because I am trying to create a portable app using embedded python zip. Pip appears to no longer work in embedded python either so I can't install via the embedded interpreter either.Veii
This was exactly my problem and I solved it with this line of code which I ran on my Mac before zipping and uploading to Lambda: pip install --platform=manylinux2014_x86_64 --only-binary=:all: --target lambda_env/lib/python3.9/site-packages pillowElevenses
@Elevenses This is the only solution that worked for me. The only changes I made were the folder structure: pip install --platform=manylinux2014_x86_64 --only-binary=:all: --target python pillowUninterrupted
Yes, something along the lines of the two comments above seems to work on Lambda pip3 install --platform=manylinux2014_x86_64 --only-binary=:all: --target ./package pillow. (Naturally, you should customize the target parameter to whatever folder you're using.)Vinnievinnitsa
F
43

I ran into this problem as well. 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). If the Pillow .egg file is there you might as well delete that too. Then re-install Pillow.

substitute "python2.7" for the version of python you're using.

Frustrated answered 14/10, 2014 at 21:17 Comment(5)
On my Mac I had to do this under /usr/local/lib/python2.7/site-packages Fixed, thanks.Promotive
On Ubuntu, I ran sudo pip3 uninstall uninstall pip3 4 times (because there was always still something to remove) until there was nothing left to remove. And then running pip3 install -U pillow --user, no more errors from Pillow.Strouse
thanks! pip uninstall pillow pip install pillow was enoughParamedical
Thanks you led me to the solution. In Ubuntu 16.04. I had problems running in Python3. It was enough for me to remove the "official" package python3-pil (i.e., sudo apt-get remove python3-pil), because I had installed Pillow via pipPettaway
Unistall and reinstall of pillow worked perfectly. pip uninstall pillow pip install -U pillowKessia
T
5

What is your version of pillow?

enter image description here

Pillow >= 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaging instead. Here's the official documentation.

Thermostatics answered 21/4, 2018 at 12:25 Comment(1)
I editet in PIL\Image.py with from PIL.Image import core as _imaging and Exception Value: cannot import name 'core'Homily
P
4

I have got the same error with Python 3.6. Upgrading Pillow did the job for me.

sudo python3.6 -m pip install Pillow --upgrade

Probably for other python versions use your version instead of 3.6.

Politicking answered 28/2, 2021 at 11:32 Comment(0)
P
4

Just uninstall pillow:

pip uninstall pillow

then install pillow again:

pip install pillow

works great

Presidentship answered 18/2, 2023 at 4:58 Comment(0)
N
2

This may be a niche solution but I was able to fix this problem on Pycharm by going to file->settings->python interpreter and clicking the upgrade symbol next to the pillow package.

Noncooperation answered 16/10, 2020 at 4:5 Comment(0)
J
2

This can happen if you're trying to run Pillow installed on a Mac in a Linux environment (for example, e.g. building an AWS Lambda on a Mac then deploying it to a Linux runtime).

To make sure you're installing it for the right platform do the following:

pip3 install --platform manylinux1_x86_64 --only-binary=:all:

The --only-binary=:all: is required when specifying --platform and the platform itself can be found by looking at https://pypi.org/project/Pillow/7.2.0/#files (for example) - the platform is the last part of the filename e.g. win32, manylinux1_x86_64, manylinux1_i686 etc.

This avoids the need to be running Linux to install the Linux build of Pillow.

Jeepers answered 2/2, 2022 at 6:48 Comment(2)
you should also ensure that pip3 is referring to the correct Python version. If your Lambda function is Python3.8, you better use pip3.8Quickly
How about just selecting the option "Specify an ARN" for your new layer and choose your preferred version from this repo link ?Tuft
F
1

For pillow to work PIL must be in /usr/local/lib/python2.7 or3/dist-packages/PIL.py.

In dist-packages PIL.py should have a folder.

  1. sudo apt-get update
  2. pip install Pillow

PIL != PiL

Filaria answered 16/8, 2014 at 14:8 Comment(4)
And btw, PIL do have a folder, under /usr/local/lib/python2.7/dist-packages/PILVarion
import PIL not import PiL.Filaria
Dude, as I said, PiL was a typo. Originally i did import PIL :D, and still it doesnt workVarion
Try python3 import PILFilaria
N
1

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:

1 - spin up an EC2 Amazon Linux instance.( i will only cover this part)

2 - Use dockers.


Short solution

  1. Install Python 3 in Amazon Linux 2 intance. (Must be python3.X you plan to use in lambda)
  2. Install a virtual environment under the ec2-user home directory.
  3. Activate the environment, and then install Boto 3.
  4. Install Pillow
  5. Create a ZIP archive with the contents of the library(PIL and Pillow.libs)
  6. Add your function code to the archive.
  7. Update your the lambda.(AWS CLI)

Long solution

  1. If Python 3 isn't already installed, then install the package using the yum package manager.
`$ sudo yum install python3 -y`
  1. Create 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/env`
  1. Activate 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 confifure see documentation ( you will need this in step 7)

3.1 Activate the environment by sourcing the activate file in the bin directory under your project directory.

  `$ 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.3 Use the pip command to install the Boto 3 library within our virtual environment.

   `pip install boto3`
  1. Install libraries with pip.

    $ pip install Pillow

4.1 Deactivate the virtual environment.

 `$ deactivate`
  1. 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.

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)

5.1 ZIP everything thats inside the PIL folder.

   `zip -r9 PIL.zip ./PIL/`

   add the Pillow.libs to your ZIP

   `zip -gr PIL.zip Pillow.libs`
  1. 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.py`
    
  2. Update 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

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

Nanice answered 21/5, 2020 at 21:48 Comment(0)
C
0

Quick solution - import PyQt5 as well, you will not get that error message.

import PyQt5

from PIL import ImageGrab

Calmative answered 5/7, 2021 at 11:38 Comment(0)
C
0

As some other answers have alluded to, this can happen when you build Pillow on MacOS and try to import PIL in another OS like some Amazon Linux flavor.

My exact use-case was to package imagehash as a Lambda layer which includes pillow as a dependency. The following guideline has worked great for me for all python packages.

  1. Install the SAM CLI SAM Installation
  2. Create your python script with the lambda handler defined
  3. Create your template.yml file with your Lambda function defined. Your CodeUri should be the relative path to your python script.
  4. Add the package you are trying to create a layer for to your requirements.txt.
  5. Run the following SAM command sam build -t path_to_template
  6. You will now have the following directory .aws-sam/build/{Logical ID Of Lambda Function}. Inside you will see that your python packages and their dependencies have been installed just as if you ran pip download package and unzipped the wheel files.

Now, the python files have been prepped by SAM specifically for Lambda and you can continue with creating your Lambda Layer as desired. Configuring Lambda Layers

Since I use AWS SAM CLI already for running Lambda functions locally, this has been the easiest method for me to create my layers.

Crackdown answered 14/9, 2021 at 23:43 Comment(0)
D
0

Uninstall and reinstall of pillow worked perfectly. pip uninstall pillow pip install pillow. This actually works.

Downtrend answered 17/5, 2023 at 17:57 Comment(0)
U
0

The only solution that worked for me was bundling the package files along with binaries as suggested by @ChrisDanger in one of the comments.

The folder structure is changed as well. Here is the final code I used:

pip install --platform=manylinux2014_x86_64 --only-binary=:all: --target python pillow

Zip the output file and upload it to the AWS Lambda Layer

Uninterrupted answered 14/7, 2023 at 6:1 Comment(0)
C
0

I wanted to add an additional solution as I ran into the same problem 11 years later.

In my case I am running Anaconda3 on a Windows 10 machine. Installed matplotlib. I go to import matplotlib and get the same error: cannot import name _imaging

In my case the answer was to install matplotlib from the conda-forge channel rather than the default channel. The version of matplotlib and pillow was the same on both channels. Clearly a dependency must have been different, but I didn't have the energy to track it down.

Calling answered 11/7 at 2:57 Comment(0)
D
-1

I'm using Flask with Google App Engine. I have the module Pillow installed via this command:

pip install -t lib pillow

I fixed this error by defined PIL in my app.yaml file:

libraries:
- name: PIL
  version: latest
Dreher answered 3/6, 2019 at 17:43 Comment(0)
F
-1

Solution

  1. pip uninstall PIL
  2. pip uninstall Pillow
  3. pip install Pillow
Fulgurous answered 13/6, 2022 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.