Linux – How to pass environment variables to a WSGI script, using uWSGI

environment-variableslinuxpythonuwsgi

I've added the following line to /etc/environment:

FOO_DEPLOYMENT_ENV="vbox"

Upon logging in via SSH, I can echo $FOO_DEPLOYMENT_ENV and, of course, see vbox output to the shell. If I open a Python shell and run os.getenv('FOO_DEPLOYMENT_ENV'), it will return 'vbox', but the same code in my Python application, when run by uWSGI (as the www-data user), it does not see the environment variable.

Clearly, this isn't a problem of uWSGI, and is rather a problem with my understanding of environment variables, or how they're properly set, and the contexts in which they can be retrieved. What am I doing or understanding incorrectly?

Best Answer

In short you can't. An environment variable is allowed to exist in a specific shell instance AND can be exported to sub-shells. But in no case can you export environment variables to the a more Parent shell and certainly not outside any inheartence chain of shells.

However, you can store entries in a file as you suggest and then import them manually by the '.' command in the event of SH derivatives or the source command by CSH derivatives.

I personally use tcsh for my interactive shell. If you were using that you could put an alias called cwdcmd that would execute a set of commands between each entered command. I use this facility to put the hostname, username, and current working directory in my command prompts. Using this facility you could put an include directive that would adjust your environment between each command prompt.