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

时间:2014-05-20 14:41:44

标签: 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

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

这是我们当前的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

从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

2 个答案:

答案 0 :(得分:2)

正如人们在您的问题评论

中提到的那样,这似乎是与道路相关的问题

可能的近视解决方案手动将所有内容添加到您的路径中 - 请在此处查看最佳答案: How to import modules in Google App Engine? 至少,这将有助于将问题缩小到与路径相关的问题。

文档说的是什么:https://developers.google.com/appengine/docs/python/

  

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

所以从我所知道的,b / c一切都在你问题中的app.yaml文件中或者下面,路径应该已经是正确的了。

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

答案 1 :(得分:0)

将以下行添加到app.yaml

库: - 名称:MySQLdb   版本:"最新"

这是在这里的文档。 https://cloud.google.com/appengine/docs/python/cloud-sql/

理想情况下,它应该已在GCM DJANGO指南中记录。本来可以节省我很多时间。

Praveen