无法解决sys路径错误;据说模块缺失
我不断抛出异常,表示未安装模块。任何帮助将不胜感激。
我环顾四周,有人建议可以通过扩展 sys 路径以包含应用程序目录来解决此错误。我非常确定 WSGI 文件中的 sys.path 指令会处理以下问题:
import os, sys
sys.path.append('/home/osqa')
sys.path.append('/home/osqa/trunk')
os.environ['DJANGO_SETTINGS_MODULE'] = 'trunk.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
错误输出如下所示:
[info] mod_wsgi (pid=15737): Attach interpreter ''.
[info] mod_wsgi (pid=15737): Create interpreter 'trunk|'.
[info] [client 172.31.0.6] mod_wsgi (pid=15737, process='OSQA', application='trunk|'):
Loading WSGI script '/home/osqa/trunk/apache/django.wsgi'.
[error] [client 172.31.0.6] mod_wsgi (pid=15737): Exception occurred processing WSGI
script '/home/osqa/trunk/apache/django.wsgi'.
[error] [client 172.31.0.6] Traceback (most recent call last):
[error] [client 172.31.0.6] File "/usr/lib/python2.6/site-
packages/django/core/handlers/wsgi.py", line 230, in __call__
[error] [client 172.31.0.6] self.load_middleware()
[error] [client 172.31.0.6] File "/usr/lib/python2.6/site-
packages/django/core/handlers/base.py", line 42, in load_middleware
[error] [client 172.31.0.6] raise exceptions.ImproperlyConfigured('Error importing
middleware %s: "%s"' % (mw_module, e))
[error] [client 172.31.0.6] ImproperlyConfigured: Error importing middleware
forum.middleware.extended_user: "No module named markdown"
Apache 配置如下所示:
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /home/osqa/trunk
ServerName trunk
CustomLog logs/osqa.access.log common
ErrorLog logs/osqa.error.log
WSGIScriptAlias / /home/osqa/trunk/apache/django.wsgi
<Directory> /home/osqa/trunk/apache>
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess OSQA
WSGIProcessGroup OSQA
Alias /m/ /home/osqa/trunk/forum/skins/
<Directory /home/osqa/trunk/forum/skins>
Order deny,allow
Allow from all
</Directory>
Alias /upfiles/ /home/osqa/trunk/forum/upfiles/
<Directory /home/osqa/trunk/forum/upfiles>
Order deny,allow
Allow from all
</Directory></VirtualHost>
forum.middleware.extended_user 如下所示: 从 django.contrib.auth.middleware 导入 AuthenticationMiddleware 从 django.contrib.auth 导入 logoutfrom forum.models.user 导入 AnonymousUser 从 forum.views.auth 导入forward_suspended_user导入日志记录
class ExtendedUser(AuthenticationMiddleware):
def process_request(self, request):
super(ExtendedUser, self).process_request(request)
if request.user.is_authenticated():
try:
request.user = request.user.user
if request.user.is_suspended():
user = request.user
logout(request)
return forward_suspended_user(request, user)
return None
except Exception, e:
import traceback
logging.error("Unable to convert auth_user %s to forum_user: \n%s" % ( request.user.id, traceback.format_exc()
))
request.user = AnonymousUser()
return None
I keep throwing an exception saying a module isn't installed that is. Any help would be much appreciated.
I've looked around and it's been suggested that this error could be solved by extending the sys path to include the app directory. Something I'm pretty sure is taken care of by the sys.path directive in the WSGI file:
import os, sys
sys.path.append('/home/osqa')
sys.path.append('/home/osqa/trunk')
os.environ['DJANGO_SETTINGS_MODULE'] = 'trunk.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The error output looks like this:
[info] mod_wsgi (pid=15737): Attach interpreter ''.
[info] mod_wsgi (pid=15737): Create interpreter 'trunk|'.
[info] [client 172.31.0.6] mod_wsgi (pid=15737, process='OSQA', application='trunk|'):
Loading WSGI script '/home/osqa/trunk/apache/django.wsgi'.
[error] [client 172.31.0.6] mod_wsgi (pid=15737): Exception occurred processing WSGI
script '/home/osqa/trunk/apache/django.wsgi'.
[error] [client 172.31.0.6] Traceback (most recent call last):
[error] [client 172.31.0.6] File "/usr/lib/python2.6/site-
packages/django/core/handlers/wsgi.py", line 230, in __call__
[error] [client 172.31.0.6] self.load_middleware()
[error] [client 172.31.0.6] File "/usr/lib/python2.6/site-
packages/django/core/handlers/base.py", line 42, in load_middleware
[error] [client 172.31.0.6] raise exceptions.ImproperlyConfigured('Error importing
middleware %s: "%s"' % (mw_module, e))
[error] [client 172.31.0.6] ImproperlyConfigured: Error importing middleware
forum.middleware.extended_user: "No module named markdown"
And the Apache Config looks like this:
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /home/osqa/trunk
ServerName trunk
CustomLog logs/osqa.access.log common
ErrorLog logs/osqa.error.log
WSGIScriptAlias / /home/osqa/trunk/apache/django.wsgi
<Directory> /home/osqa/trunk/apache>
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess OSQA
WSGIProcessGroup OSQA
Alias /m/ /home/osqa/trunk/forum/skins/
<Directory /home/osqa/trunk/forum/skins>
Order deny,allow
Allow from all
</Directory>
Alias /upfiles/ /home/osqa/trunk/forum/upfiles/
<Directory /home/osqa/trunk/forum/upfiles>
Order deny,allow
Allow from all
</Directory></VirtualHost>
forum.middleware.extended_user looks like this:
from django.contrib.auth.middleware import AuthenticationMiddleware
from django.contrib.auth import logoutfrom forum.models.user import AnonymousUser
from forum.views.auth import forward_suspended_userimport logging
class ExtendedUser(AuthenticationMiddleware):
def process_request(self, request):
super(ExtendedUser, self).process_request(request)
if request.user.is_authenticated():
try:
request.user = request.user.user
if request.user.is_suspended():
user = request.user
logout(request)
return forward_suspended_user(request, user)
return None
except Exception, e:
import traceback
logging.error("Unable to convert auth_user %s to forum_user: \n%s" % ( request.user.id, traceback.format_exc()
))
request.user = AnonymousUser()
return None
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否发布这个抛出异常的中间件,以便我们可以准确地看到它试图导入的内容?
听起来它正在导入 Markdown,而 Markdown 不在您的路径上。通常,它会安装在您的
site-packages
目录中,而不是安装在项目内部。尝试
pip install markdown
更新:你说它肯定已经安装了。 Markdown 安装在哪里?
Can you post this middleware that's throwing the exception so we can see exactly what it's trying to import?
Sounds like it's importing markdown, and markdown isn't on your path. Normally, this would be installed in your
site-packages
directory and not so much inside of your project.try
pip install markdown
Update: you said it's definitely installed. Where is markdown installed?