How to import modules in Google App Engine?

前端 未结 4 1183
礼貌的吻别
礼貌的吻别 2020-11-27 16:36

I have created a simple GAE app based on the default template. I want to add an external module like short_url. How do I do this? The directions that I have found so far are

4条回答
  •  隐瞒了意图╮
    2020-11-27 16:41

    You can import python packages as ZIPs. This allows you to avoid the maximum file count.

    The app engine docs address this.

    python 2.5: zipimport is supported.

    python 2.7: zipimport is not supported, but Python 2.7 can natively import from .zip files.

    This is how I import boto.

    sys.path.insert(0, 'boto.zip')
    import boto #pylint: disable=F0401
    from boto import connect_fps  #pylint: disable=F0401
    

    The cons of this technique include having to manually re-archive many packages.

    For example, boto.zip decompresses into the "boto" subdirectory, with the "boto" module inside of it (as another subdirectory).

    So to import boto naturally you may have to do from boto import boto, but this can cause weirdness with a lack of __init__.py.

    To solve this, simply decompress, and archive the boto subfolder manually as boto.zip, and place that in your application folder.

提交回复
热议问题