2

我将 djang1.3.1 更新为 djang1.4。
在我当地的环境中,这很好。
但是当我将我的代码输入服务器时,发生了错误!
在我的服务器中,我可以使用'python manage.py shell'
并可以'import settings',没关系,
但仍然有这个错误,谁能帮助我!

最后,我卸载了apache,安装了nginx + uwsgi,解决了这个问题......

Traceback (most recent call last):

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)

File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/modpython.py", line 180, in handler
return ModPythonHandler()(req)

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/modpython.py", line 142, in __call__
self.load_middleware()

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:

File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 184, in inner
self._setup()

File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)

File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))

ImportError: Could not import settings 'guihuame.settings' (Is it on sys.path?): No module named guihuame.settings

我的python版本

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 4, 0, 'final', 0)

使用管理.py

python manage.py shell
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import settings
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named settings
>>> import guihuame.settings
>>> 

谢谢!

我的文档树是
guihuame
-- manage.py
-- guihuame (包含init .py wsgi.py settings.py urls.py)
-- 其他应用

我的 wsgi.py 、 wsgi.py 和 settings.py 在同一个文件夹中

import os
import sys
sys.path.insert(0, '/var/web/trunk/guihuame/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "guihuame.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
4

1 回答 1

2

您需要添加一行以wsgi.pyguihuame模块的位置添加到 PYTHONPATH:

import sys
sys.path.insert(0, 'Wherever guihuame is')

更新:由您实际提供guihame. 但是,不要对路径进行字符串操作。Python 将选择正确的分隔符。如果有问题,请使用normpath.

另请注意,这os.path.split(os.path.dirname(__file__))[0]是选择您的文件系统根目录,或者更有可能只选择它的下一级。这可能不是你想要的。

于 2012-04-08T12:41:51.723 回答