Gunicorn and django settings module

django, gunicorn, python

Solution

I have the same problem, what is worse, The other project which has the same structure works well. I find django don't change the DJANGO_SETTINGS_MODULE path, so in the wsgi.py, I set it like this: os.environ['DJANGO_SETTINGS_MODULE'] = "settings" It can fix the problem.

Problem

I've got a django app (https://github.com/wjdp/nt-tickets) with the settings.py file in the root rather than in the nt-tickets subdirectory. This works fine as in wsgi.py and manage.py the following line has been altered to reflect the change: ``` os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") ``` I'm planning on using gunicorn for deployment and am running into an issue. Gunicorn ignores this line and assumes that the settings file will be `nt_tickets.settings`. I'm starting gunicorn like this: ``` gunicorn wsgi:application ``` and getting this error: ``` ImportError: Could not import settings 'nt_tickets.settings' (Is it on sys.path?): No module named nt_tickets.settings ``` I have found that putting `--settings "settings"` on the end of the command fixes the issue, but why am I having to do this? Surely the enviroment variable set in wsgi.py should suffice? This might seem petty, I'm trying to not repeat myself. Full trace: ``` 2013-07-10 01:12:12 [29417] [ERROR] Error handling request Traceback (most recent call last): File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 126, in handle_request respiter = self.wsgi(environ, resp.start_response) File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__ self.load_middleware() File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware for middleware_path in settings.MIDDLEWARE_CLASSES: File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__ self._setup(name) File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup self._wrapped = Settings(settings_module) File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__ raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)) ImportError: Could not import settings 'nt_tickets.settings' (Is it on sys.path?): No module named nt_tickets.settings ```

Original source