Django 或 mod_wsgi 在运行时会修改 sys.path 吗?

发布于 2024-10-04 18:22:59 字数 3102 浏览 2 评论 0原文

我设置了 mod_wsgi,并检查它工作正常。 我还想出了一个简单的 django 项目,并使用以下命令检查了它是否正常工作

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

但是,当我运行以下 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()

我收到了此错误。

[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 中。

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]

这是我得到的 sys.path 。它没有来自 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

因此,我将“mysite”django 项目复制到“/Library/Python/2.6/site-packages”,现在它可以正常工作了。

这有什么问题吗?即使我将 django 项目目录添加到 sys.path,django 也很可能不知道(或忽略)此路径以及 PYTHONPATH 环境变量中的路径。

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

Django 在运行时会修改 sys.path 吗?如果是这样,如何防止这种情况发生?如果不是,为什么 Django 不知道我附加的路径?

这是我正在使用的系统。

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

已解决

请参阅我的其他 PO。简而言之,我将 django 项目移至可从网络访问的 www doc 目录,一切正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-10-11 18:22:59

您只需检查“/Users/smcho/Desktop/django/mysite”是否在 sys.path 中,但如果没有,则添加它和“/Users/smcho/Desktop/django”。要导入 mysite.settings,Python 需要后者,而不是前者。所以你应该确保添加这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文