views:

689

answers:

4

I've just finished setting up a django app on virtualenv, deployment went smoothly using a fabric script, but now the .wsgi is not working, I've tried every variation on the internet but no luck. My .wsgi file is:

import os
import sys
import django.core.handlers.wsgi

# put the Django project on sys.path
root_path = os.path.abspath(os.path.dirname(__file__) + '../')
sys.path.insert(0, os.path.join(root_path, 'kcdf'))
sys.path.insert(0, root_path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'kcdf.settings'

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

I keep getting the same error:

[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] mod_wsgi (pid=16938): Exception occurred processing WSGI script '/home/kcdfweb/webapps/kcdf.web/releases/current/kcdf/apache/kcdf.wsgi'.
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] Traceback (most recent call last):
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 230, in __call__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self.load_middleware()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 33, in load_middleware
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 269, in __getattr__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._setup()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 40, in _setup
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._wrapped = Settings(settings_module)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 75, in __init__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] ImportError: Could not import settings 'kcdf.settings' (Is it on sys.path? Does it have syntax errors?): No module named kcdf.settings

my virtual environment is on /home/user/webapps/kcdfweb my app is /home/user/webapps/kcdf.web/releases/current/project_name my wsgi file home/user/webapps/kcdf.web/releases/current/project_name/apache/project_name.wsgi

+1  A: 

You need to add the directory that is two up from your wsgi file, so instead of:

root_path = os.path.abspath(os.path.dirname(__file__) + '../')

you should have

root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../', '../'))

...as your wsgi file is in a directory called apache, under your project folder.

Rob Golding
tried that it didn't work, might be worth noting that when i created a sample django project outside the virtualenv and tried to run it I got:jwesonga@kcdf:~/tester$ export DJANGO_SETTINGS_MODULE=tester.settingsjwesonga@kcdf:~/tester$ django-admin.py runserverError: Could not import settings 'tester.settings' (Is it on sys.path? Does it have syntax errors?): No module named tester.settings
jwesonga
A: 

If you're using a virtualenv, you'll need to activate it within the WSGI script to set the paths up correctly.

root_path = os.path.abspath(os.path.dirname(__file__) + '../')
activate_this = os.path.join(root_path, "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
Daniel Roseman
Use of activate_this.py in mod_wsgi is discouraged because it does fiddles with sys.prefix which technically may cause issues with some code. Read 'http://code.google.com/p/modwsgi/wiki/VirtualEnvironments'.
Graham Dumpleton
Also, activate_this is unnecessary. All you really have to do is call site.addsitedir() on the virtualenv site-packages dir.
Carl Meyer
No, site.addsitedir() is not always sufficient, especially if you didn't use --no-site-packages when creating the virtual environment. This is because site.addsitedir() adds new directories at the end of sys.path and so anything in standard Python installation still takes precedence. The purpose of the script is to reorder sys.path so new directories go at the start and take precedence. Go read the documentation referred to on mod_wsgi site.
Graham Dumpleton
A: 

Do you have an init.py file in your "kcdf" directory?

Also, you should be calling site.addsitedir() on the virtualenv's site-packages directory, if you expect to be able to import stuff from the virtualenv. See the mod_wsgi docs for details. Though if it can't even import your settings, I don't think this is your current problem.

Carl Meyer
+2  A: 

I'd recommend that you look at the docs for using Virtualenv with mod_wsgi. They offer a few alternative approaches for hooking into your virtualenv which might work better for you.

SeanOC