[英]ImportError: No module named django.core.wsgi
I'm moving a web application from a Windows environment to CentOS 5.11 and Apache, and with everything installed, I'm receiving a 500
when I try to load up the site. 我正在将一个Web应用程序从Windows环境移动到CentOS 5.11和Apache,并且安装完所有内容后,当我尝试加载该站点时,我收到的是
500
。 The error log shows this: 错误日志显示:
mod_wsgi (pid=5461): Target WSGI script '/usr/local/treehouse/wsgi/index.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=5461): Exception occurred processing WSGI script '/usr/local/treehouse/wsgi/index.wsgi'.
Traceback (most recent call last):
File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django.core.wsgi'
I found this question (and the related ones), which appears to be similar but none of the answers there fix the issue. 我发现了这个问题 (以及相关问题), 这个问题似乎很相似,但没有一个问题可以解决这个问题。 I'm not running in a
virtualenv
, and django appears to be installed correctly, as I can run the offending statement: 我没有在
virtualenv
运行,并且django似乎安装正确,因为我可以运行违规声明:
>>> from django.core.wsgi import get_wsgi_application
in the interactive Python interpreter and it works just fine . 在交互式Python解释器中它工作得很好 。 Here's the script causing the error (it's just the default
index.wsgi
that you see everywhere you look for this stuff): 这是导致错误的脚本(它只是你在任何地方看到这个东西的默认
index.wsgi
):
import os, sys
sys.path.append('/usr/local/treehouse/apple')
sys.path.append('/usr/local/treehouse/apple/apple')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apple.settings")
from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Obviously the last line causes the error, but here's the fun part: the last line remains the problem, even if I add this just above it: 显然最后一行会导致错误,但这是有趣的部分:最后一行仍然是问题,即使我在它上面添加它:
import django.core
import django.core.handlers
Both of the underlying packages import without a hitch ( django.core.wsgi
is just a thin wrapper around some functionality in django.core.handlers.wsgi
). 无论是底层的包导入顺利(
django.core.wsgi
只是在周围的一些功能的瘦包装django.core.handlers.wsgi
)。 It's only when I try to access the wsgi
packages that problems occur. 只有当我尝试访问
wsgi
包时才会出现问题。 Inspecting sys.path
within this script shows all the right directories ( /usr/local/lib/python3.4/site-packages
, etc.). 在此脚本中检查
sys.path
显示所有正确的目录( /usr/local/lib/python3.4/site-packages
等)。 And again, running the exact same code from the interactive interpreter causes no errors. 再次,从交互式解释器运行完全相同的代码不会导致错误。
I tried an uninstall/reinstall of django ( pip install django==1.6.5
), then I reset the VM to a totally clean snapshot and rebuilt/reinstalled everything , and still I get the exact same behavior. 我尝试卸载/重新安装django(
pip install django==1.6.5
),然后我将VM重置为一个完全干净的快照并重建/重新安装所有内容 ,但我仍然得到完全相同的行为。
What is going on? 到底是怎么回事?
Some of the django source files pulled down by pip
are saved with Windows line endings : that is, they end with \\r\\n
rather than \\n
. 由
pip
拉下的一些django源文件以Windows行结尾保存:也就是说,它们以\\r\\n
而不是\\n
结尾。 On *nix systems, this has the effect of breaking imports in affected files, because instead of looking for django.core.wsgi
, it tries to import django.core.wsgi\\r
. 在* nix系统上,这会影响受影响文件中的导入,因为它不会查找
django.core.wsgi
,而是尝试导入django.core.wsgi\\r
。
This is why django.core.handlers
can still be imported: the __init__.py
file is empty, so there are no line endings to corrupt. 这就是为什么仍然可以导入
django.core.handlers
: __init__.py
文件为空,因此没有行结尾可以破坏。
To fix it, run dos2unix
on affected files. 要修复它,请在受影响的文件上运行
dos2unix
。 I'm not sure how many files are actually affected (except that it's many of them) so I just hit all of them with a quick Python script from the interpreter: 我不确定实际影响了多少文件(除了它们中的很多文件),所以我只是用解释器中的一个快速Python脚本来点击所有文件:
import os
from os.path import abspath, join
for root, _, files in os.walk('/usr/local/lib/python3.4/site-packages/django'):
for f in files:
os.system('dos2unix %s' % abspath(join(root, f)))
Et voilà , no more import errors! Etvoilà ,没有更多的导入错误!
I stumbled on this issue after I began hacking on the django
source files in desperation. 在我绝望地开始攻击
django
源文件后,我偶然发现了这个问题。 I edited django/core/__init__.py
(normally empty) by adding this: 我通过添加以下内容编辑了
django/core/__init__.py
(通常为空):
from . import wsgi
I modified index.wsgi
to include this: 我修改了
index.wsgi
来包含这个:
import django.core
django.core.wsgi # no-op to see if we can access it
and ended up with a fascinating new error: 最后出现了一个引人入胜的新错误:
Traceback (most recent call last):
File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module>
import django.core
File "/usr/local/lib/python3.4/site-packages/django/core/__init__.py", line 1, in <module>
from . import wsgi
File "/usr/local/lib/python3.4/site-packages/django/core/wsgi.py", line 1, in <module>
from django.core.handlers.wsgi import WSGIHandler
ImportError: No module named 'django.core.handlers.wsgi'
So from within django/core/__init__.py
, I can access django/core/wsgi.py
. 所以从
django/core/__init__.py
,我可以访问django/core/wsgi.py
But django.core.handlers.wsgi
is out of reach. 但是
django.core.handlers.wsgi
是遥不可及的。 I deleted the changes to reproduce the original error--but the error had changed. 我删除了更改以重现原始错误 - 但错误已更改。 Now I was getting the
No module named 'django.core.handlers.wsgi'
even without the explicit relative import. 现在我得到了
No module named 'django.core.handlers.wsgi'
的No module named 'django.core.handlers.wsgi'
即使没有明确的相对导入。
This was when it hit me. 这是它击中我的时候。 I opened up
django/core/handlers/wsgi.py
in gedit, clicked the end of a line, pressed the spacebar, deleted the insertion, and saved the file. 我在gedit中打开了
django/core/handlers/wsgi.py
,单击了行的末尾,按下空格键,删除了插入,并保存了文件。 It should have been a no-op. 它应该是一个无操作。 But when I restarted the server, suddenly the import error had moved on to a different django import.
但是当我重新启动服务器时,突然导入错误已转移到另一个django导入。 The only logical explanation was that the line endings were wrong, and by re-saving the file in gedit, I was silently fixing them.
唯一合乎逻辑的解释是行结尾是错误的,通过在gedit中重新保存文件,我正默默地修复它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.