ben432rew
ben432rew

Reputation: 1902

Why does gunicorn not see the corrent environment variables?

On my production server, I've set environment variables both inside and outside my virtualenv (only because I don't understand this issue going on) including a variable HELLO_WORLD_PROD which I've set to '1'. in the python interpreter, both inside and out my venv, os.environ.get('HELLO_WORLD_PROD') == '1' returns True. In my settings folder, I have:

import os

if os.environ.get('HELLO_WORLD_PROD') == '1':
    from hello_world.settings.prod import *  # noqa
else:
    from hello_world.settings.dev import *  # noqa

Both prod.py and dev.py inherit from base.py, and in base DEBUG = False, and only in dev.py does DEBUG = True.

However, when I trigger an error through the browser, I'm seeing the debug page.

I'm using nginx and gunicorn. Why is my application importing the wrong settings file?

You can see my gunicorn conf here

Thanks in advance for your patience!

Upvotes: 2

Views: 5118

Answers (1)

ben432rew
ben432rew

Reputation: 1902

I was using sudo service gunicorn start to run gunicorn. The problem is service strips all environment variables but TERM, PATH and LANG. To fix it, in my exec line in my gunicorn.conf I added the environment variables there using the --env flag, like exec env/bin/gunicorn --env HELLO_WORLD_PROD=1 --env DB_PASSWORD=secret etc.

Upvotes: 2

Related Questions