Django + mod_wsgi + apache: ImportError at / No module named djproj.urls


I'm trying to deploy my Django application on my linode server with apache and mod_wsgi.

file: /srv/www/

import os
import sys


os.environ['PYTHON_EGG_CACHE'] = '/srv/www/'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

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

file: /etc/apache2/sites-available/


DocumentRoot /srv/www/

WSGIScriptAlias / /srv/www/

<Directory "/srv/www/">
   Order allow,deny
   Allow from all

ErrorLog /srv/www/ 
CustomLog /srv/www/ combined

When I visit / of my site I get this error:


Request Method: GET
Request URL:

Django Version: 1.3
Python Version: 2.6.6
Installed Applications:
Installed Middleware:

File "/usr/local/lib/python2.6/dist-packages/Django-1.3-    py2.6.egg/django/core/handlers/" in get_response
  101.                             request.path_info)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py    2.6.egg/django/core/" 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/" 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/" 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/" in import_module
  35.     __import__(name)

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

I can't get it to work. Ideas?

3/20/2014 5:39:57 AM

Accepted Answer

Either change all your module/package entries and imports to exclude the project name, or put /srv/www/ in sys.path as well.

3/20/2014 5:42:53 AM

I second Ignacio Vazquez-Abrams's answer. You must add the path to your project directory as well as the path to its parent directory to sys.path. Here is an example of the WSGI script file I use. I keep the file inside the project directory.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow