Bialecki
Bialecki

Reputation: 31061

Django form in Google App Engine unable to find module PIL

There are actually a couple of questions here. For what I'm doing, I'm doing a basic image upload with Django 1.1 and Google App Engine. Here is my form class:

class UploadPictureForm(forms.Form):
    picture = forms.ImageField()

And then on submit, I have the following code:

def handle_picture(request):
    form = UploadPictureForm(request.POST, request.FILES)

    if form.is_valid():
        save_picture(request.FILES['picture']

I get the following error:

Exception Type: ImportError
Exception Value: No module named PIL
Exception Location:     /Library/Python/2.6/site-packages/django/forms/fields.py in clean, line 495
Python Executable: /usr/bin/python2.6
Python Version: 2.6.1
Python Path: [..., '/Library/Python/2.6/site-packages', '/Library/Python/2.6/site-packages/PIL']

and I've installed PIL in the site-packages directory and if I run python from the command line, I can import PIL with import PIL

SO question #1 is why doesn't this work? Is GAE doing something that's keeping this from working? The second thing is that I notice GAE has some Image APIs. Should I be using those instead? All of this is somewhat new for me.

Upvotes: 2

Views: 864

Answers (3)

jbochi
jbochi

Reputation: 29644

More, I'm not quite sure you can use PIL at all with GAE. It's a C-based library and therefore it's a no-no for GAE (which requires custom packages to be pure-Python only) (there's even a ticket for this issue).

PIL is used by the development server locally to simulate all image api calls. You can found the installation instructions here

Upvotes: 0

Nick Johnson
Nick Johnson

Reputation: 101149

Can you show us the full stacktrace? Based on what you provided, there's several options:

  1. Django is trying to use PIL directly for some reason. This won't work on App Engine, as PIL isn't available there, and you can't upload it because it's C code.
  2. Your own code is trying to use PIL directly. You need to use the Images API instead.
  3. The App Engine framework is trying to use PIL, but can't locate it. The local stub for the Images API relies on PIL, so if your code is trying to use that, App Engine needs to import PIL.

Upvotes: 0

Mike Hordecki
Mike Hordecki

Reputation: 97161

For starters, you shouldn't use GAE with Python 2.6. Google App Engine is created with 2.5 in mind and it usually breaks in multiple ways on 2.6.

More, I'm not quite sure you can use PIL at all with GAE. It's a C-based library and therefore it's a no-no for GAE (which requires custom packages to be pure-Python only) (there's even a ticket for this issue).

That's what images API was created for. It still uses PIL as a backend (at least on the user side), but offers a 'safe' subset of it.

Upvotes: 3

Related Questions