views:

301

answers:

2

Has anyone got jsonpickle working on the google app engine? My logs say there is no module but there is a module as sure as you're born. i'm using jsonpickle 0.32.

<type 'exceptions.ImportError'>: No module named jsonpickle
Traceback (most recent call last):
  File "/base/data/home/apps/xxxxx/xxxxxxxxxxxxxxxxx/main.py", line 4, in <module>
    import jsonpickle
+2  A: 

As this post explains, jsonpickle requires one of a few underlying JSON modules. I would fix the issue as follows -- put at the top of your module(s) that need jsonpickle the following few lines:

import sys
import django.utils.simplejson
sys.modules['simplejson'] = django.utils.simplejson

This addresses the problem: jsonpickle needs simplejson (as one of the JSON modules it can use), but GAE has it as django.utils.simplejson, so you need to "alias" it appropriately.

Alex Martelli
hmm, i'm still seeing the same error: even after fixing your "diango" typo :). Is it possible GAE has cache that it won't let go?
cellis
@cellis, I don't see how cache could have anything to do with it -- sys.modules is the one and only "cache" for loaded modules in Python (GAE or not) and in GAE it is either keeping the modules loaded in the previous request served in this process, or building it up as it goes if this process is serving its first request. "there is no modules" cannot possibly be the **exact** error message you're seeing, so what about a copy and paste of the error traceback instead? (Pls edit your answer to supply that info!).
Alex Martelli
+3  A: 

I have managed to make it work registering django.utils.simplejson as a json encoder/decoder. In this real file index.py class Pizza is encoded and decoded back:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import jsonpickle

class Pizza:
    pass                

class Example(webapp.RequestHandler):
    def get(self):
        jsonpickle.load_backend('django.utils.simplejson',
                                'dumps','loads',ValueError)
        encoded = jsonpickle.encode(Pizza())
        self.response.out.write( jsonpickle.decode(encoded).__class__ )

run_wsgi_app(webapp.WSGIApplication([('/', Example),],debug=True))
Nulldevice
This is the preferred solution: http://jsonpickle.github.com/api.html#choosing-and-loading-backends
John Paulett