问题描述
我已经遇到这个问题了很短时间,根本找不到任何地方的解决方案。我正在使用Google App Engine运行通过PyCharm创建的Django 1.5(通过GAE SDK)的默认Python 2.7应用程序。我可以成功上传应用程序,但访问实际页面时,我会收到服务器错误。然后,检查Google App Engine中的日志,我看到:
I have been running into this problem for a short while now and simply can't find a solution anywhere. I am using Google App Engine to run a default Python 2.7 app with Django 1.5 (via GAE SDK) created through PyCharm. I can upload the app successfully, but upon visiting the actual page, I get a Server Error. Then, checking the logs in Google App Engine, I see this:
ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application
在互联网搜索一段时间后,我找到了几个帖子解决这个问题,但试图让他们似乎没有解决我的问题。例如:问题已通过用app替换application,如下所示:
After searching the internet for a while, I was able to find a few posts which address this issue, but attempting them never seemed to solve my problem. For example: This problem was solved by replacing "application" with "app" in the following lines:
application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)
事实上,我遇到了同样的问题这个解决方案在过去为我提供了一个修复,但是当时我正在运行一个单独的应用程序,并没有通过GAE。
In fact, I had run into this same issue before and this solution provided a fix for me in the past, however at that time I was running a separate app and it was not through the GAE.
我查看了Django文档对于版本1.5 ,但代码和建议,我似乎与我目前在项目中的内容相冲突。
I checked the Django documentation for version 1.5 here, but the code and suggestions there don't seem to conflict with what I currently have in my project.
我更多地阅读了这种类型的问题,并看到另一篇文章,建议检查应用程序的wsgi。 py文件确保它被命名为应用程序或'app',以便在应用程序的其余部分中可以使用相同的名称。但是,在查看这些设置后,我看到应用程序也在那里使用:
I read a bit more about this type of problem, and saw another post that suggested checking the app's wsgi.py file to ensure that it is named 'application' or 'app' respectively, so that one could then use that same name throughout the rest of the application. However, upon checking those settings I saw that 'application' was used there too:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
在settings.py中甚至有一行使用相同的命名声明WSGI应用程序:
There's even a line in settings.py which uses the same nomenclature to declare the WSGI application:
WSGI_APPLICATION = 'Chimera.wsgi.application'
我真的很麻烦调试这个。我得到的感觉真的很笨,我只是看不到,但不幸的是,我不是特别擅长这种东西 - 我仍然是这个领域的一个新手。
I'm really having trouble debugging this. I get the feeling it's really dumb and I just can't see it, but unfortunately I'm not particularly good at this kind of stuff -- I'm still a bit of a novice in this field.
有没有人知道我可以尝试解决这个问题?
Does anyone have any idea what I could try in an attempt to fix this issue?
更新:我开始逐行更改并测试的东西,最终我发现GAE日志根据app.yaml下的脚本的输入变化。所以如果我在main.app和main.application之间的处理程序下更改脚本,它会分别调整日志输出以引用app或application。所以在app.yaml文件中的行告诉应用程序要查找什么,但是我仍然看不到为什么找不到它。不知道还有什么可以改变来测试它。我希望我更多地了解实际的内部工作,以便我可以弄清楚为什么应用程序对该属性感到困惑。是否尝试运行它甚至被实例化或某些东西?
UPDATE: I started making line by line changes and testing things, and eventually I found that the GAE log changes depending on the input for the "script" under app.yaml. So if I change the script under "handlers" between "main.app" and "main.application", it adjusts the log output to refer to "app" or "application" respectively. So that line in the app.yaml file tells the app what to look for, but I'm still not seeing why it can't be found. Not sure what else I could change to test it out. I wish I knew a bit more about the actual inner workings so that I could figure out why the app is confused about the attribute. Is it trying to run before it even gets instantiated or something?
源代码如下:
main.py
import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'Chimera.settings'
from google.appengine.ext.webapp import util
from django.conf import settings
settings._target = None
import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher
def main():
application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
app.yaml
application: eloquent-ratio-109701
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.application
libraries:
- name: django
version: 1.5
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Chimera.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
来自GAE的完整日志: / p>
Full log from GAE:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 302, in _LoadHandler
raise err
ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application
感谢您的帮助。
推荐答案
在您的 main.py
文件(即 main
模块)应用程序
是 main()
函数内的一个变量,而不是主要
模块。基本上你不需要一个 main()
函数。
In your main.py
file (i.e. the main
module) application
is a variable inside the main()
function, not an attribute of the main
module. Basically you don't need a main()
function.
GAE有一些特定的支持,使用Django,I强烈建议您通过文档和 Django应用示例。
GAE has some specific support for using Django, I'd strongly suggest going through the Django Support documentation and the Django App example.
这篇关于模块“主”没有属性应用程序 - Google App Engine Python与Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!