Friday, September 19, 2014

Gunicorn error when running with Django1.7

If you running your django1.7 apps with Gunicorn, you will get this error:

Traceback (most recent call last):
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/", line 507, in spawn_worker
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/workers/", line 114, in init_process
    self.wsgi =
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/", line 66, in wsgi
    self.callable = self.load()
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/", line 105, in load
    mod = util.import_module("")
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/", line 20, in <module>
    from import get_validation_errors
ImportError: No module named validation

There are several ways to fix this:

1. A quick fix (should be fixed in the official gunicorn source code) if your run your django app using django_gunicorn:

* Open the <your_virtualenv_path>/local/lib/python2.7/site-packages/gunicorn/app/ file of gunicorn and change as following:

#from import get_validation_errors

def make_wsgi_application():
    # validate models
    s = StringIO()
#    if get_validation_errors(s):
#        error =
#        sys.stderr.write("One or more models did not validate:\n%s" % error)
#        sys.stderr.flush()

#        sys.exit(1)

    import django
    from import BaseCommand
    cmd = BaseCommand()
    import sys
    cmd.stdout, cmd.stderr = sys.stdout, sys.stderr

    if django14:
        return get_internal_wsgi_application()
    return WSGIHandler()


2. Running your django app using gunicorn instead of django_gunicorn:

$ gunicorn mydjangoproject.wsgi:application