Does Django or mod_wsgi modify sys.path when it's running?
我设置mod_wsgi,并检查它工作正常。
我还提出了简单的django项目,并使用以下命令检查它是否正常工作
1 | django-admin.py runserver --settings=mysite.settings |
但是,当我运行以下wsgi时,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import os import sys mysite = '/Users/smcho/Desktop/django/mysite' if mysite not in sys.path: sys.path.insert(0,'/Users/smcho/Desktop/django/mysite') django = '/Users/smcho/Desktop/django' if django not in sys.path: sys.path.insert(0,'/Users/smcho/Desktop/django') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() |
我收到了这个错误。
1 | [Wed Nov 24 10:36:45 2010] [error] [client 127.0.0.1] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings |
通过使用mod_wsgi运行以下python代码,我了解到"/Library/Python/2.6/site-packages"位于sys.path中。
1 2 3 4 5 6 7 8 9 10 11 12 13 | def application(environ, start_response): status = '200 OK' output = 'Hello World! WSGI working perfectly! '*10 for i in sys.path: output += str(i) + ' ' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] |
这是我得到的sys.path。它没有PYTHONPATH的路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /Library/Python/2.6/site-packages/ply-3.3-py2.6.egg ... /Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload /Library/Python/2.6/site-packages ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode |
所以,我将'mysite'django项目复制到'/Library/Python/2.6/site-packages',现在它可以正常工作了。
这有什么问题?很可能即使我将我的django项目目录添加到sys.path,django也不知道(或忽略)此路径和PYTHONPATH环境变量的路径。
1 2 | sys.path.insert(0,'/Users/smcho/Desktop/django/mysite') sys.path.insert(0,'/Users/smcho/Desktop/django') |
Django在运行时修改了sys.path吗?如果是这样,如何防止这种情况?如果没有,为什么Django不知道我的路径?
这是我正在使用的系统。
1 2 3 | OS : Mac OS X 10.6.5 Python : 2.6.1 Django : 1.2.3 |
解决了
请参考我的其他PO。简而言之,我将我的django项目移动到可从Web访问的www doc目录,一切正常。
您只检查'/ Users / smcho / Desktop / django / mysite'是否在