Pillow: ImportError: cannot import name '_imaging'

Created on 10 Apr 2018  Β·  23Comments  Β·  Source: python-pillow/Pillow

What did you do?

Importing Pillow as a part of a flask project.

What did you expect to happen?

Pillow to be imported properly.

What actually happened?

File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 60, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging'

What versions of Pillow and Python are you using?

I'm using Python 3.5 and Pillow 5.1.0 on Ubuntu 16.

Most helpful comment

get the same error running Pillow on AWS Lambda.
Is it possible to have Pillow on such kind of runtima?

All 23 comments

By the way, I have already tried to remove both PIL and pillow and then reinstalling them.

>>> from PIL import _imaging
>>> 

Still getting the same error. This is in the PIL package that was pip installed along with Pillow, by the way. More context of the Image.py file:

try:
    # If the _imaging C module is not present, Pillow will not load.
    # Note that other modules should not refer to _imaging directly;
    # import Image and use the Image.core variable instead.
    # Also note that Image.core is not a publicly documented interface,
    # and should be considered private and subject to change.
    from PIL import _imaging
    if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
        raise ImportError("The _imaging extension was built for another "
                          "version of Pillow or PIL:\n"
                          "Core version: %s\n"
                          "Pillow version: %s" %
                          (getattr(core, 'PILLOW_VERSION', None),
                           PILLOW_VERSION))

Please can you paste the full traceback?

How did you install?

You don't want to install PIL separately, only pip install pillow.

@nchouard You should pip install Pillow (or apt-get install python-pillow) then run Python then >>> from PIL import Image (or _imaging) using the _same Python installation_ (i.e. make sure you don't have multiple versions of Python installed). You don't need to be doing anything "in the PIL package that was pip installed along with Pillow"… that's not relevant here.

Traceback (most recent call last):
[Tue Apr 10 15:00:53.665168 2018] [wsgi:error] [
  File "/var/www/parleyback/parleyback.wsgi", line 6, in <module>
[Tue Apr 10 15:00
    from app import create_app
[Tue Apr 10 15:00:53.665179 2018] [wsgi:error] [pid 
  File "/var/www/parleyback/app/__init__.py", line 4, in <module>
[Tue Apr 10 15:00
    from . import extensions, config, user, auth, voice, audio, site, call, payment
  File "/var/www/parleyback/app/user/__init__.py", line 2, in <module>
[Tue Apr 10 1
    from app import api
[Tue Apr 10 15:00:53.665196 2018] [wsgi:error] [pid 30736] 
  File "/var/www/parleyback/app/api.py", line 3, in <module>
[Tue Apr 10 15:00:53.6
    from app import utils
[Tue Apr 10 15:00:53.665205 2018] [wsgi:error] [pid 30736
  File "/var/www/parleyback/app/utils/__init__.py", line 6, in <module>
[Tue Apr 10
    from PIL import Image
[Tue Apr 10 15:00:53.665213 2018] [wsgi:error] [pid 30736
  File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 60, in <module>

    from . import _imaging as core
[Tue Apr 10 15:00:53.665231 2018] [wsgi:error] [
ImportError: cannot import name '_imaging'

Pillow was installed using pip. When I install pillow, however, it installs PIL as well. Is this an issue?

@aclark4life Ok, thanks. That is exactly what I do. The issue seems to be stemming from the error in the PIL/Image.py file though...

@nchouard If you look at the source code right above line 60 you'll see this::

try:
    # If the _imaging C module is not present, Pillow will not load.
    # Note that other modules should not refer to _imaging directly;
    # import Image and use the Image.core variable instead.
    # Also note that Image.core is not a publicly documented interface,
    # and should be considered private and subject to change.

I think next you should paste the output of pip install Pillow as @hugovk suggested (although the wsgi:error is interesting … is this mod_wsgi by any chance?)

Yes, this is mod_wsgi.

Collecting Pillow
  Downloading Pillow-5.1.0-cp36-cp36m-manylinux1_x86_64.whl (2.0MB)
    100% |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2.0MB 753kB/s
Installing collected packages: Pillow
Successfully installed Pillow-5.1.0

The _imaging file is present under the name: _imaging.cpython-36m-x86_64-linux-gnu.so

@nchouard If you can from PIL import Image in _Python_ then this is likely a mod_wsgi issue e.g. https://stackoverflow.com/a/43671174/185820

It looks like you're right, it's an issue with mod_wsgi and what it was compiled against. Thanks a lot for your help!

I though this is about an imaging library. Why do I need an Apache module?

The mod_wsgi package provides an Apache module that implements a WSGI compliant interface for hosting Python based web applications on top of the Apache web server.

@nise you don't, this is just a discussion about the problem that @nchouard was having when running Pillow in a Python hosted web application

get the same error running Pillow on AWS Lambda.
Is it possible to have Pillow on such kind of runtima?

I had the same error as pedrodparkes on running pillow with Lambda, any fixes?

@gweiying None that I know of, unless you, or someone reading this, are providing one in a pull request. Also, this ticket is closed? So presumably already fixed in latest Pillow, or being addressed in another issue somewhere.

The original issue was solved, please open a new issue with details, or you may get a quicker answer on Stack Overflow:

I ran into this problem using Pillow with Serverless + AWS Lambda too

@patricio-ezequiel-hondagneu-roig did you find a solution?

I fixed an issue by preparing Lambda archive on Linux. It fails on Mac in the way described above.

i tried pip uninstall pillow multiple times, and then did pip install pillow.
It worked!

I had the same problem with aws lambda when i update to python3.8, in my case a deployed using zappa==0.51.0 and Pillow==7.2.0, it turn out a forgot to change my runtime to "runtime": "python3.8" on the zappa_settings.json. I hope it helps someone else

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxhumber picture maxhumber  Β·  3Comments

FlowerCode picture FlowerCode  Β·  4Comments

boskicthebrain picture boskicthebrain  Β·  4Comments

hxzhao527 picture hxzhao527  Β·  4Comments

nomarek picture nomarek  Β·  3Comments