AppRegistryNotReady:尚未加载应用。 Django的

时间:2016-01-29 14:09:05

标签: python django google-app-engine

我试图在谷歌应用引擎上运行django 1.9。尝试通过Google API Explorer访问API时出现以下错误。

Traceback (most recent call last):
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 96, in LoadObject
    __import__(cumulative_path)
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/services.py", line 9, in <module>
    from cityguide.api.internal.categories import Categories
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/internal/categories.py", line 10, in <module>
    from cityguide.models import Category
  File "/home/gemini/projects/cityguide-backend/src/cityguide/models.py", line 8, in <module>
    class ContactDetails(models.Model):
  File "/home/gemini/projects/cityguide-backend/src/lib/django/db/models/base.py", line 94, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/gemini/projects/cityguide-backend/src/lib/django/apps/registry.py", line 239, in get_containing_app_config
    self.check_apps_ready()
  File "/home/gemini/projects/cityguide-backend/src/lib/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.

我已添加

builtins:
- deferred: on
- remote_api: on
- django_wsgi: on

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

env_variables:
  DJANGO_SETTINGS_MODULE: 'mysite.settings'

app.yaml文件中。

wsgi.py看起来像

import os

from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = get_wsgi_application()

我尝试在django.setup()的顶部添加models.py行,但它显示了不同的错误。

ERROR    2016-02-01 10:03:02,918 wsgi.py:263] 
Traceback (most recent call last):
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 96, in LoadObject
    __import__(cumulative_path)
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/services.py", line 9, in <module>
    from cityguide.api.internal.categories import Categories
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/internal/categories.py", line 10, in <module>
    from cityguide.models import Category
  File "/home/gemini/projects/cityguide-backend/src/cityguide/models.py", line 6, in <module>
    django.setup()
  File "/home/gemini/projects/cityguide-backend/src/lib/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/gemini/projects/cityguide-backend/src/lib/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/gemini/projects/cityguide-backend/src/lib/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/home/gemini/projects/cityguide-backend/src/lib/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/gemini/projects/cityguide-backend/src/lib/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/gemini/projects/cityguide-backend/src/cityguide/admin.py", line 2, in <module>
    from cityguide.models import Category
ImportError: cannot import name Category
ERROR    2016-02-01 10:03:02,919 wsgi.py:263] 
Traceback (most recent call last):
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/gemini/softwares/google_appengine/google/appengine/runtime/wsgi.py", line 96, in LoadObject
    __import__(cumulative_path)
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/services.py", line 9, in <module>
    from cityguide.api.internal.categories import Categories
  File "/home/gemini/projects/cityguide-backend/src/cityguide/api/internal/categories.py", line 10, in <module>
INFO     2016-02-01 10:03:03,000 module.py:794] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 500 -
    from cityguide.models import Category
INFO     2016-02-01 10:03:03,001 module.py:794] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 500 -
  File "/home/gemini/projects/cityguide-backend/src/cityguide/models.py", line 6, in <module>
INFO     2016-02-01 10:03:03,001 module.py:794] default: "GET /_ah/api/discovery/v1/apis HTTP/1.1" 500 60
    django.setup()
INFO     2016-02-01 10:03:03,001 module.py:794] default: "GET /_ah/api/discovery/v1/apis HTTP/1.1" 500 60
  File "/home/gemini/projects/cityguide-backend/src/lib/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/gemini/projects/cityguide-backend/src/lib/django/apps/registry.py", line 78, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant

此问题的临时解决方案:

似乎我需要先打开主页。所以它会加载数据库,因为我让主页返回所有数据库表内容。在主页上显示数据库列表后,我们就可以调用Google API Explorer了。

有没有办法改进这个解决方案?

2 个答案:

答案 0 :(得分:22)

首先应该在脚本中以这种方式启动django:

import django
django.setup()

请参阅https://docs.djangoproject.com/en/1.9/ref/applications/#django.setup

您还可以查看链接的“疑难解答”部分,了解解决此特定异常的其他可能性。

通过在services.py文件的顶部添加以上两行来解决这个问题..

答案 1 :(得分:0)

在你的脚本中启动django as:

from django_root_app.wsgi import application