Google App Engine在缺少环境变量DJANGO_SETTINGS_MODULE时中止

Google App Engine在缺少环境变量DJANGO_SETTINGS_MODULE时中止

问题描述:

这是 Google App Engine和Django支持的后续问题

本教程适用于一个空项目,但是当我尝试将现有的Django应用程序部署到Google App Engine时,它会开始抛出错误:

The tutorial works great for an empty project, however when I try to deploy an existing Django app to Google App Engine, it starts throwing errors:

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 223, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/wsgi.py", line 219, in __call__
    self.load_middleware()
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

另一个可能是相关的:

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 223, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/wsgi.py", line 219, in __call__
    self.load_middleware()
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 95, 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 settings

根据本教程,我修改了我的settings.py。该应用程序部署没有问题,syncdb也工作;数据库和所有必需的表都已到位。

I have modified my settings.py as per the tutorial. The app deploys without problems and the syncdb works as well; the database and all required tables are in place.

我发现这个参考到日志中显示的错误消息,但建议的修复没有帮助。

I found this reference to the error message as shown in the log but its suggested fix did not help.

任何想法可能导致这种情况?

Any ideas what might be causing this?

编辑:

缩短这个已经很长的问题,我删除了以前发布的 wsgi.py 文件。我没有与这个问题有关。

To shorten this already lengthy question I removed the previously posted wsgi.py file. I had nothing to do with this issue.

编辑2:

认为我做了一个小的改进,GAE正在抱怨:

I think I made a small improvement and GAE is now complaining:

ImportError: No module named properties

我认为我的app.yaml文件中可能有一个条目丢失,但是我不知道哪个文件可能是。我发现一些关于缺少模块的参考,但没有一个反映这个错误消息的参考...

I presume there might be an entry missing in my app.yaml file but I have no clue as to which file that may be. I found some references regarding missing modules but none that reflect this error message...

经过多次尝试和错误,我设法解决这个问题,尽管部分是:

After much trial and error I managed to solve this, albeit partially:

我没有设法获取我现有的 PyCharm 项目部署(尚未)。因为我热衷于在GAE上运行一个版本,所以我去了一个不同的方法,并在我的本地机器上创建了一个新的空的Django项目( project 2 )。

I did not manage to get my existing PyCharm project deployed (yet). As I was keen to have a version running on GAE I went for a different approach and created a new empty Django project (project 2) on my local machine.

我首先使用本教程运行正常。然后我将我现有的PyCharm项目中的 models.py 文件复制到 project 2 ,并添加了一个名为 properties 它使用 models.py 文件。最初这是错误的,因为GAE不断抱怨,找不到我的属性应用程序。随之而来,它突然出现在我的管理员身上。我不知道是什么造成了初始问题,但现在正在工作。

I first deployed the blank project using this tutorial which ran fine. I then copied my models.py file from my existing PyCharm project to project 2 and added an app called properties which uses the models.py file. Initially this went wrong as GAE kept complaining it could not find my properties app. After fiddling about with it, it suddenly appeared in my admin. I'm not sure what caused the initial problem but it's working now.

对我来说,成功至关重要:

Things crucial to success for me:

我将以下内容添加到我的settings.py的顶部:

I added the following to the very top of my settings.py:

import os
ROOT_PATH = os.path.dirname(__file__)

...然后替换为默认的 STATIC_ROOT with:

... and then replaced the default STATIC_ROOT with:

STATIC_ROOT = ROOT_PATH + os.sep + 'static'

然后我跑了:

python manage.py collectstatic

项目2的目录。这收集了Django管理员使用的所需的静态文件(图像文件,css文件等)。

from the dir of project 2. This collects the required static files which Django admin uses (image files, css files etc)

所以,我目前有一个在GAE上运行的原始项目的精简版本。缺少的东西:

So, I currently have a stripped down version of my original project running on GAE. Things that are missing:


  • 原始项目使用 grappelli 更风格的管理界面。我不知道这是否会在GAE上运行。 编辑:在GAE上部署Grappelli是很容易的。只需将Grappelli包复制到您的项目目录,然后运行 manage.py collectstatic 。还可以根据记录添加grappelli urls。重新部署。完成

  • The original project used grappelli for a more styled admin interface. I'm not sure whether this will run on GAE. Deploying Grappelli on GAE is reasonbly easy. Just copy the Grappelli package to your project dir and run manage.py collectstatic. Also add grappelli urls as per documention. Redeploy. Done.

原始项目使用名为 templated_email 的模块发送模板化电子邮件从我的属性应用程序。与 grappelli 相同:我不知道GAE是否支持此功能。希望它会运行在GAE上,因为它对我的具体应用至关重要。 编辑:我碰到了这个小宝石使得templateated_email过时,并允许我插入GAE邮件系统。

The original project uses a module called templated_email to send out templated emails from my properties app. Same as grappelli: I'm not sure whether this is supported by GAE. Hopefully it will run on GAE as it is crucial for my specific app. I bumped into this little gem which makes templated_email obsolete and lets me plug into GAE mail system.

说实话方法部分地违反了PyCharm的好处,它应该全面支持Django和GAE开箱即用。

To be honest, this approach partially defies the benefits of PyCharm which should fully support Django and GAE out of the box. It will probably require some additional research and tweaking to get that up and running.

以上任何意见或见解都显然得到欣赏!

Any comments or insights re the above are obviously appreciated!