使用Django的AppEngine应用程序无法加载

mig*_*elv 6 python django google-app-engine python-2.7

Django不断导致我们的应用程序崩溃.部署后,应用程序运行正常,但是一旦初始化实例重新启动/关闭,它通常无法启动时出现类似于以下内容的错误:


Traceback (most recent call last):  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in call
    self.load_middleware()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 53, in load_middleware
    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware myfolder.middleware: "No module named myfolder.middleware"

我们的文件结构类似于:


|- app.yaml
|- _ _ init _ _.py
|- settings.py
|- myfolder |
|           |- _ _ init _ _.py
|           |- middleware.py
|           |- ...
|-...
|

我们的app.yaml:


application: XXXXX
module: app
version: master
runtime: python27
api_version: 1
threadsafe: true

handlers: - url: /api/(login|logout|passwd|master.|banners.) script: app.handler secure: always ...

builtins: - django_wsgi: on

libraries: - name: django version: 1.5

env_variables: DJANGO_SETTINGS_MODULE: 'settings'

我们的应用程序中有2个模块,它们都表现出这种行为(它们具有类似的配置).有时模块会在再次崩溃之前保持一整天.加载失败后,所有后续请求都会因同样的错误而失败.再多部署一次总能暂时解决问题.

我们使用简单的django和CloudSql.该问题在开发服务器中无法重现.部署后,两个模块中的所有内容都可正 所有中间件,ndb,memcache,cloudsql,taskqueue等,包括"myfolder"中的所有模块和其他所有库xcopied.

以下尝试解决此问题的方法无效:

  • 我们尝试使用appengine_config.py强制django从django.conf导入设置重新加载设置\nsettings._target = None \n
  • 最初我们在"myfolder"中共享了设置,并在root settings.py中导入了"from myfolder.shared_settings import*",但django无法加载模块myfolder.shared_settings(类似问题)
  • 使用自定义mysettings.py并在app.yaml或python中定义DJANGO_SETTINGS_MODULE

该系统还没有上线,但很快就会出现问题.

类似失败配置的其他痕迹:


Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
Run Code Online (Sandbox Code Playgroud)

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 239, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 353, in __getattr__
    self._update_configs()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 289, in _update_configs
    self._registry.initialize()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 164, in initialize
    import_func(self._modname)
  File "/base/data/home/apps/s~blue-myapp/app:master.375531077560785947/appengine_config.py", line 17, in 
    settings._target = None
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/utils/functional.py", line 227, in __setattr__
    self._setup()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
Run Code Online (Sandbox Code Playgroud)

这是我们当前的appengine_config.py:


import sys
import logging

logging.debug(",\n".join(sys.path))

# Since Google App Engine's webapp framework uses Django templates, Django will half-initialize when webapp is loaded.
# This causes the initialization of the rest of Django's setting to be skipped. If you are getting this error, you need
# to explicitly force Django to reload your settings:

from django.conf import settings
settings._target = None
Run Code Online (Sandbox Code Playgroud)

从appengine_config.py记录sys.path在成功的实例启动和失败的实例启动之间不会改变(当然除了XXXXXXXXXXX位):


/base/data/home/apps/s~blue-persomi/app:master.3759720XXXXXXXXXXX,
/base/data/home/runtimes/python27/python27_dist/lib/python27.zip,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/plat-linux2,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-tk,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-old,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-dynload,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/site-packages,
/base/data/home/runtimes/python27/python27_lib/versions/1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/yaml-3.10
Run Code Online (Sandbox Code Playgroud)

Fra*_*llo 2

正如人们在您的问题评论中提到的那样,这似乎是一个与路径相关的问题

可能的短视解决方案将所有内容手动添加到您的路径中 - 请查看此处的最佳答案: 如何在 Google App Engine 中导入模块? 至少,这将有助于将问题缩小到与路径相关的范围。

文档所说的:https ://developers.google.com/appengine/docs/python/

Python 模块包含路径包含应用程序的根目录(包含 app.yaml 文件的目录)。您在应用程序根目录中创建的模块可使用根目录中的路径来使用。不要忘记在子目录中创建init .py 文件,这样 Python 会将子目录识别为包。

因此,据我所知,b/c 所有内容都位于问题中的 app.yaml 文件处或下方,路径应该已经正确。

  1. 仔细检查所有__init__.py文件是否就位且拼写正确。
  2. 尝试删除所有*.pyc文件并重新生成它们。
  3. 尝试从容器的文件夹名称导入FOLDER_CONTAINING_YAML.myfolder.middleware