google app engine (python): ImportError no module named django

荒凉一梦 提交于 2019-12-06 13:12:59

Came up with the following solution:

Get django 1.1 and put it under your project root.

Add an empty file "non_gae_indicator" to your project root folder.

Add django and non_gae_indicator to your app.yaml skip_files element:

skip_files:
- ^(.*/)?app\.yaml
- ^(.*/)?app\.yml
- ^(.*/)?index\.yaml
- ^(.*/)?index\.yml
- ^(.*/)?#.*#
- ^(.*/)?.*~
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
- ^(.*/)?.*\.bak$
- ^django
- ^non_gae_indicator

Now we have a way to tell whether we are running under the GAE-sdk or live - since non_gae_indicator won't be available when we are live.

So in main.py you can do:

if not os.path.exists(os.path.abspath(os.path.dirname(__file__)) + '/non_gae_indicator'):
    # GAE
    from google.appengine.dist import use_library
    use_library('django', '1.1')
else:
    # Not GAE - Add our django package to the path
    sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)) + '/django')

You should run your local SDK server with the --allow_skipped_files flag (or else the skipped files will appear to not be exist when checking them - the server console gives a warning about it).

@stallarida - The problem is that .96 is shipped as default with the SDK. What I did in the end, which is a dirty hack but works, is to update the version of django in the appengine directory to 1.1. Worked fine, needed a bit of tweaking between dev and production. Specifically I had to comment out use_library('django', '1.1') when running locally but include it when uploading my app.

I'm sure there's a better solution and I'll work it out when my linux experience improves.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!