Django + mod_wsgi + apache:/ Error模块名为djproj.urls的ImportError

时间:2011-04-30 11:48:18

标签: python django importerror django-wsgi

我正在尝试使用apache和mod_wsgi在我的linode服务器上部署我的Django应用程序。

file: /srv/www/example.com/djproj/django.wsgi

import os
import sys

sys.path.append('/srv/www/example.com/djproj')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

file: /etc/apache2/sites-available/example.com

/etc/apache2/sites-available/example.com
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com

DocumentRoot /srv/www/example.com/public_html

WSGIScriptAlias / /srv/www/example.com/djproj/django.wsgi

<Directory "/srv/www/example.com/djproj">
   Order allow,deny
   Allow from all
</Directory>

ErrorLog /srv/www/example.com/logs/error.log 
CustomLog /srv/www/example.com/logs/access.log combined

当我访问/访问我的网站时,我收到错误

Environment:


Request Method: GET
Request URL: http://example.com/

Django Version: 1.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-    py2.6.egg/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py    2.6.egg/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /
Exception Value: No module named djproj.urls

我无法让它发挥作用。想法?

6 个答案:

答案 0 :(得分:35)

我是第二个Ignacio Vazquez-Abrams的answer。您必须将项目目录的路径以及其父目录的路径添加到sys.path。这是我使用的WSGI脚本文件的示例。我将文件保存在项目目录中。

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))

os.environ["DJANGO_SETTINGS_MODULE"] = "PROJECT_NAME.settings"

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

答案 1 :(得分:25)

更改所有模块/包条目和导入以排除项目名称,或将/srv/www/site.com放在sys.path中。

答案 2 :(得分:9)

借调ayaz的回答。您指定的路径位于搜索路径的开头非常重要,这意味着您需要使用插入..

这是我的。当我做'追加'时,我遇到了间歇性的问题。这使它坚如磐石。希望这会有所帮助。

sys.path.insert(0, '/srv/www/Appname')
sys.path.insert(1, '/srv/www')

答案 3 :(得分:5)

尝试遵循本教程 - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/

确保将Django目录和Django项目目录添加到系统路径。你的wsgi.py应该是

import os,sys

#comments
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
sys.path.append('/path/to/your/django/directory/django_project')
sys.path.append('/path/to/your/django/directory')

#comments
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

答案 4 :(得分:2)

为我纠正此错误的原因 -

从以下位置更改settings.py:

ROOT_URLCONF ='urls'

对此:

ROOT_URLCONF ='myproject.urls'

答案 5 :(得分:1)

djproj.urls可能只需urls