WSGI module and Django settings locations
<p>I'm using Django with uWSGI and trying to organise my web server config files as follow:</p>

<pre><code>/proj/
    web_config/
        docker-compose.py
        ...
        prod/
            nginx.conf
            wsgi.py
            uwsgi_params
            uwsgi.ini
            ...
    app/
        __init__.py
        settings.py
</code></pre>

<p>My problem is I don't know how to reference the final django settings module from my WSGI module:</p>

<p><strong>uwsgi.ini</strong></p>

<pre><code>[uwsgi]
# ...    
module = wsgi
env = DJANGO_SETTINGS_MODULE=(../../)app.settings # &lt;- Problem here
</code></pre>

<p><strong>wsgi.py</strong></p>

<pre class="lang-py prettyprint-override"><code>import os

from django.core.wsgi import get_wsgi_application

#                                               v- Or problem here -v
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "(../../)app.settings")
application = get_wsgi_application()
</code></pre>

<p>Is there a way to point to the right <code>app.settings</code><br>
without placing <code>wsgi.py</code> in <code>/proj/</code> or <code>/proj/app</code> ?</p>

<p>In this case, to reference to django settings, all you need to do is chdir from your <code>uwsgi.ini</code> to <code>/proj/</code> directory. Now load your <code>wsgi.py</code> file (you can keep it in your <code>app/</code> subdirectory, it doesn't contain any settings) from it using <code>module</code> uWSGI setting and set <code>DJANGO_SETTINGS_MODULE</code> to <code>app.settings</code>, without anything else (it is enough to set it only in <code>wsgi.py</code> file).</p>

<p>It should look like this (if you've moved your <code>wsgi.py</code> back to <code>app/</code>):</p>

<p>uwsgi.ini:</p>

<pre><code>[uwsgi]
# ...
chdir = /proj/
module = app.wsgi
</code></pre>

<p>wsgi.py</p>

<pre><code>import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
application = get_wsgi_application()
</code></pre>

<p>If you don't want (for some reason) to keep your <code>wsgi.py</code> file in <code>app/</code>, your <code>uwsgi.py</code> should have:</p>

<pre><code>[uwsgi]
# ...
chdir = /proj/
module = web_config.prod.wsgi
</code></pre>

<p>But <code>web_config</code> and <code>prod</code> should be proper python modules (in python 2 they must have <code>__init__.py</code> file inside). Content of <code>wsgi.py</code> doesn't change.</p>

python django wsgi uwsgi