wsgi modify sys.path when it's running?

I setup mod_wsgi, and checked it works fine. I also came up with simple django project, and also checked it works fine with the following command

django-admin.py runserver --settings=mysite.settings

However, when I run the following wsgi,

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()

I got this error.

[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

By running following python code with mod_wsgi, I learned that "/Library/Python/2.6/site-packages" is in sys.path.

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World! WSGI working perfectly!n'*10
    for i in sys.path:
        output += str(i) + 'n'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

And this is the sys.path that I got. It doesn't have the path from PYTHONPATH.

/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

So, I copied 'mysite' django project to '/Library/Python/2.6/site-packages', and now it works OK.

What's wrong with this? It's very likely that even though I added my django project directory to sys.path, django doesn't know (or disregards) this path and the paths from PYTHONPATH environment variable.

    sys.path.insert(0,'/Users/smcho/Desktop/django/mysite')
    sys.path.insert(0,'/Users/smcho/Desktop/django')

Does Django modifies sys.path when it's running? If so, how to prevent this? If not, why Django doesn't know about my appened path?

This is the system that I'm using.

OS : Mac OS X 10.6.5
Python : 2.6.1
Django : 1.2.3

SOLVED

Please refer to my other PO. In short, I moved my django project to www doc directory that is accessible from web, everything's OK.


You only check that '/Users/smcho/Desktop/django/mysite' is in sys.path , but if not you add that and the '/Users/smcho/Desktop/django'. To import mysite.settings , Python needs the latter, not the former. So you should be sure to add that.

链接地址: http://www.djcxy.com/p/9414.html

上一篇: 什么建立与Python的sys.path,什么时候?

下一篇: wsgi在运行时修改sys.path?