wjdp
wjdp

Reputation: 1478

Gunicorn and django settings module

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

Upvotes: 6

Views: 6111

Answers (3)

Deifyed
Deifyed

Reputation: 61

Under python3.4, putting a __init__.py file inside my settings directory fixed the problem.

Upvotes: 3

fang_dejavu
fang_dejavu

Reputation: 626

I fixed the problem by moving my wsgi.py file out of my django_project folder. so for the case mentioned by OP ('nt_tickets.settings'). he needs to move the wsgi.py file out of the nt_tickets folder, and that should do the trick.

Upvotes: 0

chenchiyuan
chenchiyuan

Reputation: 151

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.

Upvotes: 4

Related Questions