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/arbiter.py", line 507, in spawn_worker
    worker.init_process()
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 114, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 66, in wsgi
    self.callable = self.load()
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/djangoapp.py", line 105, in load
    mod = util.import_module("gunicorn.app.django_wsgi")
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/.venv/ptc/local/lib/python2.7/site-packages/gunicorn/app/django_wsgi.py", line 20, in <module>
    from django.core.management.validation 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/django_wsgi.py file of gunicorn and change as following:

...
#from django.core.management.validation import get_validation_errors
...

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

#        sys.exit(1)

    import django
    from django.core.management.base import BaseCommand
    django.setup()
    cmd = BaseCommand()
    import sys
    cmd.stdout, cmd.stderr = sys.stdout, sys.stderr
    cmd.check()


    translation.activate(settings.LANGUAGE_CODE)
    if django14:
        return get_internal_wsgi_application()
    return WSGIHandler()

...


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

$ gunicorn mydjangoproject.wsgi:application



References:
[0] http://www.snip2code.com/Snippet/81958/Possible-fix-to-gunicorn-s-Django-1-7-bu
[1] http://www.rkblog.rk.edu.pl/w/p/first-impressions-django-17-beta-upgrade-young-project/ 


Comments