应用程序引擎构建在出错时中止环境变量DJANGO_SETTINGS_MODULE未定义

时间:2015-01-06 02:33:29

标签: python django google-app-engine

我正在尝试使用他们的推送部署教程https://cloud.google.com/tools/repo/push-to-deploy-quickstart将django 1.6项目部署到谷歌应用引擎。该项目在本地工作得非常好但是当我推动该项目时,应用引擎似乎会在导入django模型时回落到django 0.96,如下面的错误所示。为什么会这样?

======================================================================
    ERROR: Failure: EnvironmentError (Environment variable DJANGO_SETTINGS_MODULE is undefined.)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 414, in loadTestsFromName
        addr.filename, addr.module)
      File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
        return self.importFromDir(dir_path, fqname)
      File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
        mod = load_module(part_fqname, fh, filename, desc)
      File "/var/jenkins/workspace/mango_app/apps/merchant/__init__.py", line 1, in <module>
        from gateways import *
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/__init__.py", line 1, in <module>
        from plugnpay import PlugnPay
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/plugnpay.py", line 5, in <module>
        from apps.merchant.exceptions import MissingDataError
      File "/var/jenkins/workspace/mango_app/apps/merchant/__init__.py", line 1, in <module>
        from gateways import *
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/__init__.py", line 1, in <module>
        from plugnpay import PlugnPay
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/plugnpay.py", line 6, in <module>
        from apps.merchant.lib.api import PostGateway
      File "/var/jenkins/workspace/mango_app/apps/merchant/lib/api.py", line 6, in <module>
        from apps.merchant.gateways.core import Gateway
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/core.py", line 7, in <module>
        from apps.merchant.gateways.models import PlugnpayResponseModel
      File "/var/jenkins/workspace/mango_app/apps/merchant/gateways/models.py", line 1, in <module>
        from django.db import models
      File "/google-cloud-sdk/platform/google_appengine/lib/django-0.96/django/db/__init__.py", line 7, in <module>
        if not settings.DATABASE_ENGINE:
      File "/google-cloud-sdk/platform/google_appengine/lib/django-0.96/django/conf/__init__.py", line 28, in __getattr__
        self._import_settings()
      File "/google-cloud-sdk/platform/google_appengine/lib/django-0.96/django/conf/__init__.py", line 53, in _import_settings
        raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
    EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

的app.yaml

application: instance-id
version: 1
runtime: python27
api_version: 1
threadsafe: true

env_variables:
  DJANGO_SETTINGS_MODULE: 'myproject.settings'

handlers:
- url: .*
  script: myproject.wsgi.application

wsgi.py

import sys


sys.path.insert(0,'./Lib/site-packages')
sys.path.insert(1,'./apps')

import os


#Delete current django version in production
for key in [key for key in sys.modules if key.startswith('django')]:
  del sys.modules[key]


from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()


def application(environ, start_response):
    try:
        os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
    except Exception:
        pass

    return _application(environ, start_response)

我还尝试将这行代码放在appengine_config.py中:

os.environ['DJANGO_SETTINGS_MODULE'] = 'mango.settings'

目录结构

C:.
│   .gitignore
│   app.yaml
│   appengine_config.py
│   manage.py
│   requirements.txt
│   __init__.py
├───apps
├───lib
│   __init__.py
├───myproject
│   app.py
│   settings.py
│   urls.py
│   views.py
│   wsgi.py
│   __init__.py

1 个答案:

答案 0 :(得分:0)

App Engine不包含Django 1.6。您需要将其添加到项目中并上传。或者,您可以将Django 1.5与GAE一起使用,将此添加到您的app.yaml:

libraries:
- name: django
  version: "1.5"

否则,您将使用非常旧的版本(0.96),这是几年前的GAE默认版本。如果您打算使用自己的django,而不是GAE附带的1.5,我推荐使用django-nonrel。

通过执行上述任一操作,您可以消除生产中的django删除(在wsgi.py中),这很可能导致您的错误。