我一直在用WSGI开发部署在Apache服务器上的Django web应用程序,一切都进展顺利.今天,为了定制内置的Django管理界面,我对我的应用程序的admin.py做了一些小改动,最初出现了一个语法错误(未闭合的括号).这意味着,当我touch wsgi.py并加载代码时(我的虚拟主机上以守护程序模式运行WSGI),我的网站被替换为一个内部服务器错误,因为WSGI在遇到语法错误时停止.

因此,我修复了语法错误,使用manage.py check判断了没有更多的错误,并触及wsgi.py进行重新部署.但是我的网站仍然显示内部服务器错误!判断Apache日志(log),我看到的是:

[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.pypopulate方法抛出.

谷歌搜索此错误消息返回的信息少得令人惊讶,这些信息都不是来自Django文档.显然,如果你在settings.py个应用中给一个应用命名两次,这种情况有时会发生,但我不会这么做.更重要的是,自从网站正常运行以来,我没有更改过settings.py--我唯一更改的是admin.py.

我try 恢复我所做的所有更改,所以我的所有Python代码都回到了网站运行时的状态--当我试图让WSGI重新加载代码时,我仍然得到populate() isn't reentrant错误!

我也试过在settings.py的installed_apps部分注释掉不同的应用程序,即使只启用了‘django.contri.staticfiles’,错误仍然会发生.奇怪的是,即使我注释掉all个应用程序,我仍然收到错误--Django即使没有加载任何应用程序也会抛出错误!

有人知道这里发生了什么吗?或者有没有更好的方法来调试这个错误,因为Apache日志(log)中的回溯非常没有帮助?

注意:我使用的是Django 1.7、Apache 2.2和Python 2.7.

推荐答案

这是由Django设置中的某个错误引起的.不幸的是,Django在这个通用的、无用的错误消息背后隐藏了错误.

要揭示真正的问题,请打开django/apps/registry.py并在第80行附近替换:

raise RuntimeError("populate() isn't reentrant")

使用:

self.app_configs = {}

这将允许Django继续加载,并显示实际错误.

由于几种不同的原因,我遇到了这个错误.有一次是因为我的一个应用程序的admin.py中有一个错误的导入.

Django相关问答推荐

查询在对象中使用django查询与一个包含句子

AttributeError:';ManyToOneRel&39;对象没有属性';attname';

如何在Django REST框架中使用FactoryBoy创建不同的项目

我无法在Django模板中使用Get_Foo_Display

Django REST框架中如何从另一个视图调用API视图?

反向 url django 管理员

Django 身份验证系统修改

如何在 Django 中创建一个独特的 slug

删除所有实例后,Django 模型实例主键不会重置为 1

Django 模型将外键设置为另一个模型的字段

将网络测功机添加到 Heroku django 应用程序时如何克服Coudn't find that formation错误?

如何测试某个日志(log)消息是否记录在 Django 测试用例中?

Django聚合:仅求和返回值?

Django:使用render或render_to_response时添加响应头

在 Django 应用程序之间共享模型

超过 1 个外键

从基于类的通用视图中获取 request.session

Python 社区里的小马是怎么回事?

在 PyPy 下运行 Django 的缺点?

Numpy 数组到 base64 并返回到 Numpy 数组 - Python