0

Just updated GAE Python dev environment from 1.7.0 to 1.7.7, and hit an error:

ImportError: No module named json

So I created a minimum test case, here's the app.yap:

application: myapp

version: 1
api_version: 1
runtime: python27
threadsafe: true  

libraries:
- name: jinja2
  version: "2.6"

handlers:
- url: /test.*
  script: test.app
  login: required

- url: /.*
  script: routes.app
  login: required

and the test.py:

import webapp2
import json

class MainHandler(webapp2.RequestHandler):

    def get(self):
        self.response.out.write("test.py")

app = webapp2.WSGIApplication([('/test', MainHandler)],
    debug=True)

Switching dev environments is perfectly consistent - the error is thrown on 1.7.7 but not 1.7.0 (running dev environment from terminal on Linux). I've not tried uploading this to a production environment. It occurred while attempting to upgrade from Python 2.5 to 2.7, so may be due to my unfamiliarity with 2.7.

cbootle
  • 169
  • 1
  • 3
  • 8

2 Answers2

2

It was a noobie mistake. As mentioned, I was upgrading from 2.5 to 2.7, during which I moved from importing simplejson to json.

During 2.5 development, I had created a file called json.py, which of course caused a conflict when upgrading to 2.7 and trying to import json, so I renamed that file. Sorry guys, I should have mentioned that, but if I had done so, I would have realized... the json.pyc was still hanging around!

Removing the json.pyc file fixed it. However, it doesn't explain why the presence of json.pyc affected 1.7.7 but not 1.7.0 - switching between the two was perfectly repeatable.

cbootle
  • 169
  • 1
  • 3
  • 8
0

You can check for simplejson, as in this post, except I think in GAE you get simplejson from django.utils:

try: from django.utils import simplejson as json
except ImportError: import json

But the Python 2.7 runtime should allow for import json. If the above worked, you were probably running on 2.5.

Community
  • 1
  • 1
MichaelJCox
  • 756
  • 7
  • 17
  • Yep, had tried that (without try block), and get ImportError: No module named django.utils – cbootle Apr 22 '13 at 16:47
  • Yes, the dev environment, dev_appserver, under Linux (Ubuntu 12.04) with Python 2.7. – cbootle Apr 22 '13 at 20:31
  • You can't get a ImportError: No module named django.utils if you are import json with `import json` and you are running python2.7 and an out of the box 1.7.7 SDK. – Tim Hoffman Apr 23 '13 at 10:19