0

I have a strange error when trying to use Pillow to save an image in my Django admin...

from PIL import _imaging as core
ImportError: cannot import name _imaging

File Models.py:

from django.db import models
from django.contrib.auth.models import User
from string import join
import os
from PIL.Image import core as _imaging
from PIL import Image as PImage
from my_site.settings import MEDIA_ROOT

class Image(models.Model):
    title       = models.CharField(max_length=60, blank=True, null=True)
    image       = models.FileField(upload_to="images/")
    created     = models.DateTimeField(auto_now_add=True)
    width       = models.IntegerField(blank=True, null=True)
    height      = models.IntegerField(blank=True, null=True)
    category    = models.ForeignKey(Category, blank=True, null=True)
    def save(self, *args, **kwargs):
        super(Image, self).save(*args, **kwargs)
        im = PImage.open(os.path.join(MEDIA_ROOT, self.image.name))
        self.width, self.height = im.size
        super(Image, self).save(*args, **kwargs)

File Admin.py

class GenericImageAdmin(admin.ModelAdmin):
    list_display    = ["__unicode__", "title", "created", "thumbnail"]

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        obj.save()

admin.site.register(GenericImage, GenericImageAdmin)

The weirdest part is I get the same error when I remove

from PIL.Image import core as _imaging

I'm using Pillow, not the old PIL. What is the deal?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • `from PIL import _imaging as core` is not the same as `from PIL.Image import core as _imaging`. It's not completely clear which code is erroring... If you run python directly in the shell/command line where you're running django from, can you recreate the error by doing the import manually? If so can you post that (what you're typing and the resulting error and stacktrace)? – Tom Dalton Apr 16 '15 at 22:27
  • `In [4]: from PIL import Image --------------------------------------------------------------------------- ImportError Traceback (most recent call last) in () ----> 1 from PIL import Image /usr/local/lib/python2.7/dist-packages/PIL/Image.py in () ---> 63 from PIL import _imaging as core 64 if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): 65 raise ImportError("The _imaging extension was built for another " ImportError: cannot import name _imaging` – codyc4321 Apr 16 '15 at 22:44
  • worst part is, I did exactly what the tutorial says to do...http://pillow.readthedocs.org/en/latest/installation.html I uninstalled PIL already, I just doublechecked – codyc4321 Apr 16 '15 at 22:48
  • I get the same error when I comment out the overriding `from PIL.Image import core as _imaging` – codyc4321 Apr 17 '15 at 00:27
  • Duplicate? http://stackoverflow.com/questions/26720968/pil-importerror-the-imaging-extension-was-built-for-another-version-of-pillow – m9_psy Apr 17 '15 at 00:31
  • I'm getting a diff error message, but I will try out the solution there as well – codyc4321 Apr 17 '15 at 00:52
  • @m9_psy, sadly that solution didn't work. I uninstalled and reinstalled as suggested. The only piece of code exposed is `from PIL import Image as PImage` causing a very frustrating `from PIL import _imaging as core ImportError: cannot import name _imaging` – codyc4321 Apr 17 '15 at 01:00
  • Ref: http://pillow.readthedocs.org/en/latest/installation.html, this part of the stack trace "63 from PIL import _imaging as core" looks suspicious. The docs say that `_imaging` isn't part of Pillow, and they also say that PIL and Pillow aren't compatible. Is it possible you have (the original) PIL installed, or you have installed PIllow over the top of PIL? Ref the actual Pillow code that is giving you the error: https://github.com/python-pillow/Pillow/blob/master/PIL/Image.py#L63 It looks like you installation of Pillow is probably broken in some unexpected way. – Tom Dalton Apr 17 '15 at 22:24
  • All I can suggest is trying to uninstall both Pillow and PIL, then manually deleting the /usr/local/lib/python2.7/dist-packages/PIL directory, and then trying a fresh install. You might also have more luck installing Pillow into a clean virtual environment in case there is something else in your system python install that is conflicting with it. – Tom Dalton Apr 17 '15 at 22:26

0 Answers0