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

时间:2022-10-30 23:12:30

Django is constantly causing our application to crash. After deployment the application is running fine, but once the initial instance is restarted/shutdown it often fails to start with an error similar to the following:

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"

Our file structure is similar to this:

我们的文件结构类似于:


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

Our 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 ...

处理程序: - url:/api/(login|logout|passwd|master.|banners。)脚本:app.handler安全:始终...

builtins: - django_wsgi: on

builtins: - django_wsgi:on

libraries: - name: django version: 1.5

库: - 名称:django版本:1.5

env_variables: DJANGO_SETTINGS_MODULE: 'settings'

env_variables:DJANGO_SETTINGS_MODULE:'settings'

We have 2 modules in our application and they both exhibit this behaviour (they have similar configurations). Sometimes the modules will stay up for a whole day before crashing again. After they fail to load, all subsequent requests fail with he same error. Deploying one more time always solves the problem temporarily.

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

We are using plain django with CloudSql. The problem is not reproducible in the development server. After deployment everything in both modules works fine. All middleware, ndb, memcache, cloudsql, taskqueue, etc, including all the modules inside the "myfolder" and every other library xcopied.

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

The following attempts at solving this problem haven't worked:

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

  • We have tried using the appengine_config.py to force django to reload the settings with from django.conf import settings\nsettings._target = None\n
  • 我们尝试使用appengine_config.py强制django从django.conf导入设置重新加载设置\ nsettings._target = None \ n

  • Originally we had shared settings inside "myfolder" and were importing them with "from myfolder.shared_settings import *" inside the root settings.py but django could not load the module myfolder.shared_settings either (similar problem)
  • 最初我们在“myfolder”*享了设置,并在root settings.py中导入了“from myfolder.shared_settings import *”,但django无法加载模块myfolder.shared_settings(类似问题)

  • using a custom mysettings.py and defining the DJANGO_SETTINGS_MODULE in the app.yaml or in python
  • 使用自定义mysettings.py并在app.yaml或python中定义DJANGO_SETTINGS_MODULE

The system is not live yet but will be soon and we are running out of options.

该系统还没有上线,但很快就会出现,我们的选择已经不多了。

Other traces of similarly failing configurations:

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


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

This is our current appengine_config.py:

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

Logging sys.path from appengine_config.py does not change between a successful instance start and a failed instance start (apart from the XXXXXXXXXXX bit of course):

从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 个解决方案

#1


2  

It seems to be a path related issue as people have mentioned in your question's comments

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

Possible short-sighted solution add everything to your path manually - look at the top answer here: How to import modules in Google App Engine? At the very least, this will help narrow the problem to path related.

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

what the docs say: https://developers.google.com/appengine/docs/python/

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

The Python module include path includes your application's root directory (the directory containing the app.yaml file). Modules you create in your application's root directory are available using a path from the root. Don't forget to create init.py files in sub-directories, so Python will recognize the sub-directories as packages.

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

so from what I can tell, b/c everything is at or below the app.yaml file in your question the path should already be correct.

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

  1. double check all your __init__.py file files are in place and spelled correctly.
  2. 仔细检查所有__init__.py文件文件是否到位并拼写正确。

  3. try deleting all of your *.pyc files and letting them be regenerated.
  4. 尝试删除所有* .pyc文件并重新生成它们。

  5. try importing from the container's folder name FOLDER_CONTAINING_YAML.myfolder.middleware
  6. 尝试从容器的文件夹名称FOLDER_CONTAINING_YAML.myfolder.middleware导入

#2


0  

Add the following lines to your app.yaml

将以下行添加到app.yaml

libraries: - name: MySQLdb version: "latest"

库: - 名称:MySQLdb版本:“最新”

It is in the documentation here. https://cloud.google.com/appengine/docs/python/cloud-sql/

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

Ideally it should have been documented in the GCM DJANGO guide. Would have saved me a lot of time.

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

Praveen

#1


2  

It seems to be a path related issue as people have mentioned in your question's comments

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

Possible short-sighted solution add everything to your path manually - look at the top answer here: How to import modules in Google App Engine? At the very least, this will help narrow the problem to path related.

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

what the docs say: https://developers.google.com/appengine/docs/python/

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

The Python module include path includes your application's root directory (the directory containing the app.yaml file). Modules you create in your application's root directory are available using a path from the root. Don't forget to create init.py files in sub-directories, so Python will recognize the sub-directories as packages.

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

so from what I can tell, b/c everything is at or below the app.yaml file in your question the path should already be correct.

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

  1. double check all your __init__.py file files are in place and spelled correctly.
  2. 仔细检查所有__init__.py文件文件是否到位并拼写正确。

  3. try deleting all of your *.pyc files and letting them be regenerated.
  4. 尝试删除所有* .pyc文件并重新生成它们。

  5. try importing from the container's folder name FOLDER_CONTAINING_YAML.myfolder.middleware
  6. 尝试从容器的文件夹名称FOLDER_CONTAINING_YAML.myfolder.middleware导入

#2


0  

Add the following lines to your app.yaml

将以下行添加到app.yaml

libraries: - name: MySQLdb version: "latest"

库: - 名称:MySQLdb版本:“最新”

It is in the documentation here. https://cloud.google.com/appengine/docs/python/cloud-sql/

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

Ideally it should have been documented in the GCM DJANGO guide. Would have saved me a lot of time.

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

Praveen