views:

120

answers:

2

I'm using a modified version on juno (http://github.com/breily/juno/) in appengine. The problem I'm having is I have code like this:

import juno
import pprint

@get('/')
def home(web):
  pprint.pprint("test")

def main():
  run()
if __name__ == '__main__':
  main()

The first time I start the app up in the dev environment it works fine. The second time and every time after that it can't find pprint. I get this error:

AttributeError: 'NoneType' object has no attribute 'pprint'

If I set the import inside the function it works every time:

@get('/')
def home(web):
  import pprint
  pprint.pprint("test")

So it seems like it is caching the function but for some reason the imports are not being included when it uses that cache. I tried removing the main() function at the bottom to see if that would remove the caching of this script but I get the same problem.

Earlier tonight this code was working fine, I'm not sure what could have changed to cause this. Any insight is appreciated.

A: 

Is it possible you are reassigning the name pprint somewhere? The only two ways I know of for a module-level name (like what you get from the import statement) to become None is if you either assign it yourself pprint = None or upon interpreter shutdown, when Python's cleanup assigns all module-level names to None as it shuts things down.

Peter Hansen
A: 

I would leave it that way. I saw a slideshare that Google put out about App Engine optimization that said you can get better performance by keeping imports inside of the methods, so they are not imported unless necessary.

Matt Williamson