views:

169

answers:

3

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.

+3  A: 

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.

Mike Hordecki
This all makes sense. I've been meaning to switch to 2.5, but just haven't had a chance yet. As for using PIL, that makes sense. I can just as easily use the Images API. What's surprising is that I get a module not found error. Is the idea here that GAE just ignores modules that it knows are C based? Just curious here, the answer's not super important.
Bialecki
Wow, funny story! Can you even `import PIL` in Python's REPL? I've had PIL installed via easy_install, and I couldn't! Then I recompiled the whole thing from sources and bingo! It works now. (in both cases `import Image` worked flawlessly).
Mike Hordecki
A: 

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.
Nick Johnson
A: 

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

jbochi