开发者

Python App Engine import issues after app is cached

I'm using a modified version on juno (http://github.com/breily/juno/) in Google App Engine. 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜