Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created December 7, 2010 19:04
Show Gist options
  • Save slinkp/732228 to your computer and use it in GitHub Desktop.
Save slinkp/732228 to your computer and use it in GitHub Desktop.
Generic mod_wsgi script for Django in a virtualenv
"""
evolved over a couple of deployment of a couple apps...
dropping it here so i don't forget it.
Drop this somewhere in your virtualenv, edit the two environment vars,
and you're done.
"""
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'CHANGE ME'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/CHANGEME-python-eggs'
import sys
import site
# Some libraries (eg. geopy) have an annoying habit of printing to stdout,
# which is a no-no under mod_wsgi.
# Workaround as per http://code.google.com/p/modwsgi/wiki/ApplicationIssues
sys.stdout = sys.stderr
HERE = os.path.abspath(os.path.dirname(__file__))
# Try to find a virtualenv in our parent directories.
env_root = HERE
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" % sitepackages_root)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment