gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:49 25 4
gpt4 key购买 nike

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

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

我遇到了这个错误。

[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]

这是我得到的 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

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

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

这是我正在使用的系统。

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

已解决

请引用我的其他PO .简而言之,我将我的 django 项目移动到可从网络访问的 www doc 目录,一切正常。

最佳答案

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

关于python - Django 或 mod_wsgi 在运行时会修改 sys.path 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com