我一直在用 WSGI 开发部署在 Apache 服务器上的 Django Web 应用程序,一切都进展顺利。今天,我对我的应用程序的 admin.py
做了一些小的修改,试图定制内置的 Django Admin 界面,并且最初出现了一个语法错误(一个未闭合的括号)。这意味着当我触摸到 wsgi.py
并加载代码时(我的虚拟主机上的 WSGI 运行在守护进程模式下) ,我的网站被替换为一个内部服务器错误,因为 WSGI 在遇到语法错误时停止了。
因此,我修复了语法错误,检查了 manage.py check
中没有更多的错误,并触摸了 wsgi.py
以重新部署。但是我的网站仍然显示一个内部服务器错误!查看阿帕奇的日志,我看到了这个:
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Create interpreter 'quotes.cs.cornell.edu|'.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/extra/www/html/quotes/quotes_django' to path.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/' to path.
[Sun Nov 23 13:52:46 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/extra/www/html/quotes/quotes_django/
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] application = get_wsgi_application()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] django.setup()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 115, in populate
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] app_config.ready()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/apps.py", line 22, in ready
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] self.module.autodiscover()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/__init__.py", line 23, in autodiscover
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] autodiscover_modules('admin', register_to=site)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/utils/module_loading.py", line 74, in autodiscover_modules
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] import_module('%s.%s' % (app_config.name,
module_to_search))
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/usr/lib64/python2.7/importlib/__init__.py", line
37, in import_module
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] __import__(name)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/extra/www/html/quotes/quotes_django/quotespage/
admin.py", line 25
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] approve_quotes.short_description = "Approve selected
quotes"
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] ^
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] SyntaxError: invalid syntax
[Sun Nov 23 13:53:36 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] File "/extra/www/html/quotes/quotes_django/
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] application = get_wsgi_application()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] django.setup()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 78, in populate
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] raise RuntimeError("populate() isn't reentrant")
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] RuntimeError: populate() isn't reentrant
第一系列错误显示 WSGI 由于 admin.py
中的语法错误而失败。然而,第二系列错误似乎显示了 Django 内部的一个错误:
RuntimeError: populate() isn't reentrant
从 registry.py
的 populate
方法抛出。
在 Google 中搜索这条错误消息,只能得到很少的信息,这些信息都不是来自 Django 文档。显然,如果你在 settings.py
中给一个应用程序命名两次,有时会发生这种情况,但我不会这么做。更重要的是,自从网站运行良好以来,我没有改变过 settings.py
——我唯一改变的就是 admin.py
。
我尝试恢复我所做的所有更改,所以我所有的 Python 代码都回到了网站工作时的状态——当我尝试让 WSGI 重新加载代码时,我仍然会得到 populate() isn't reentrant
错误!
我还尝试过在 settings.py
的 INSTALLED _ APPS 部分注释掉不同的应用程序,即使只启用了“ django.Contrib.staticfiles”,错误仍然会发生。奇怪的是,我仍然得到错误,即使我注释出 所有的应用程序-Django 抛出的错误,即使它没有加载任何应用程序!
有人知道这是怎么回事吗?或者有什么更好的方法来调试这个错误,因为 Apache 日志中的回溯非常没有帮助?
注意: 我使用的是 Django 1.7、 Apache 2.2和 Python 2.7。