2
votes

I've read a considerable number of posts on the issue of wsgi not seeing the Django settings.py file and the best I can conclude is that my Django project is not in my PYTHONPATH.

Complicating things is that I'm running an older version of Django at the system level but this project needs to be run in a virtualenv.

I've set up a virtualenv at /usr/local/pythonenv/election and my project is located at /usr/local/pythonenv/election/src/dev/election

My .wsgi file is in a /usr/local/pythonenv/election/src/dev/election/config/

I can run the Django server using $ python manage.py runserver

But when using apache and mod_wsgi, I get the error:

ImportError: Could not import settings 'election.settings' (Is it on sys.path? Does it have syntax errors?): No module named election.settings

Here's my .wsgi file:

import os
import sys
import site

sys.stdout = sys.stderr

HERE = env_root = os.path.abspath(os.path.dirname(__file__))
found = False
while env_root != '/':
    env_root = os.path.abspath(os.path.dirname(env_root))
    if os.path.exists(os.path.join(env_root, 'bin', 'activate')):
        found = True
        break
assert found, "didn't find a virtualenv in any parent of %s" % HERE

sitepackages_root = os.path.join(env_root, 'lib')
assert os.path.exists(sitepackages_root), "no such dir %s" % sitepackages_root
for d in os.listdir(sitepackages_root):
    if d.startswith('python'):
        site.addsitedir(os.path.join(sitepackages_root, d, 'site-packages'))
        break
else:
    raise RuntimeError("Could not find any site-packages to add in %r" % env_root)

os.environ['DJANGO_SETTINGS_MODULE'] = 'election.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/election-python-eggs'

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

Here's the traceback:

[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 33, in load_middleware
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/utils/functional.py", line 269, in __getattr__
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     self._setup()
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 40, in _setup
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     self._wrapped = Settings(settings_module)
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 75, in __init__
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122] ImportError: Could not import settings 'election.settings' (Is it on sys.path? Does it have syntax errors?): No module named election.settings
1

1 Answers

3
votes

Where are you adding '/usr/local/pythonenv/election/src/dev' to sys.path?

You have also made the rest of your WSGI file more complicated than it needs to be.

Make sure you go watch:

http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

Also read:

http://code.google.com/p/modwsgi/wiki/VirtualEnvironments

and use the activate_this method for activating the virtual environment as mentioned in the presentation.