Django ignoring environment variables when run via uWSGI
Asked Answered
B

1

10

I have a django website, that uses an environment variable, DJANGO_MODE to decide which settings to use - development or staging. The environment variable is in the bashrc and when running the app using the development server, everything works fine.

But when I run the app using uWSGI, it doesn't seem to notice the environment variable and uses the default(development) settings instead of production.

I run uWSGI in Emperor mode, and other than the environment variable ignoring, everything seems to be running fine. And yes, the user running uWSGI is the same for which the bashrc has DJANGO_MODE set.

The command used to run uWSGI is -

exec  uwsgi --emperor /etc/uwsgi/vassals --uid web_user --gid --web_user

And the ini file for the vassal -

[uwsgi]
processes = 2
socket = /tmp/uwsgi.sock
wsgi-file = /home/web_user/web/project_dir/project/wsgi.py
chdir = /home/web_user/web/project_dir
virtualenv = /home/web_user/.virtualenvs/production_env
logger = syslog
chmod-socket = 777
Bryner answered 28/8, 2013 at 12:27 Comment(0)
W
16

It cannot work as bash config files are read by the bash. You have to set var in the emperor or in the vassal (the second one is a better approach). Just add

env=DJANGO_MODE=foobar

to your config (do not use whitespace).

Wappes answered 28/8, 2013 at 14:35 Comment(3)
add some print to check if DJANGO_MODE is available in os.environ, maybe you have some other kind of problem. Take in account DJANGO_SETTINGS_MODULE is set at the very beginning so the check has to happen in wsgi.pyWappes
Doh! I had the env set with spaces in it - env = DJANGO_MODE = mode, which was causing problems.Bryner
I have the same problem. However, I don't want to put env vars into uWSGI config, since manage.py also needs them. stackoverflow.com/questions/23670960Censer

© 2022 - 2024 — McMap. All rights reserved.