Can't access Environment Variables Remote Server Django
Question: On my remote server, running Ubuntu 14.04, with server configuration: Django,
Nginx and Gunicorn, I can't access environment variables although set under
the right user and server restarted.

On my local box, I set the environment variables, and django can access during
startup and works nicely. On the remote server, although the environment
variables are in place, django complains of keyerror.

    
    
    # from two scoops
    
    import os
    from django.core.exceptions import ImproperlyConfigured
    
    def get_env_variable(var_name):
        """Get the environment variable or return exception"""
        try:
            return os.environ[var_name]
        except KeyError:
            error_msg = "Set the {} environment variable".format(var_name)
            raise ImproperlyConfigured(error_msg)
    

On the server, the user running the django instance is the same user in which
the environment variables were set to.

Everything works on the local PC. On the remote machine, it doesn't. I've
restarted the server, still.

When I run `export` in terminal on the remote server, I get the list of
environment variables including what I customly set.

To confirm, i went into python to verify.

    
    
    import os
    os.environ['THE_CUSTOM_KEY']
    >> returns the Key
    

If Django **is** Python, then what step am I missing, because python sees the
environment variables?

**Edit** : I set the variables in the `~/.profile` file of the user account.

My ~/.profile with the variables

    
    
    # top part cut for brevity
    export LOG_ECG_DB="name"
    export LOG_ECG_EMAIL="email@gmail.com"
    export LOG_ECG_EMAIL_PW="password"
    export LOG_ECG_PW="password"
    export LOG_ECG_SECRET="secret_key"
    


Answer: Eventually, below is how I solved the issue. Might help someone. Inspired by
example in two scoops

Steps in brief:

  * Created a json file in ~/.env
  * Lets settings.py read the json file upon load
  * Keys from the file are used to fill in the sensitive parts

## created json file

I put the file in ~/.env of both my local and remote machine, with the content

    
    
    {
    "SECRET_KEY": "my_secret_key",
    "DB": "db_name",
    "USER": "db_user",
    "USER_PW": "db_user_password",
    
    "EMAIL": "email@email.com",
    "EMAIL_PW": "password"
    }
    

If your app isn't sending any emails, you might not need the last two lines in
the above snippet

## Read json

In my settings.py (or you could put into a utils.py file, whatever), I have
this snippet:

    
    
    import json
    from os.path import expanduser
    from django.core.exceptions import ImproperlyConfigured
    
    # with this, no need to hardcode
    # home directory
    home_directory = expanduser("~")
    
    with open(home_directory + "/.env") as f:
        secrets = json.loads(f.read())
    
    
    def get_secret(setting, secrets=secrets):
        """Get the secret variable or return explicit exception."""
        try:
            return secrets[setting]
        except KeyError:
            error_msg = "Set the {0} environment variable".format(setting)
            raise ImproperlyConfigured(error_msg)
    

## Fill in the pieces

Like so `SECRET_KEY = get_secret('SECRET_KEY')`

and so on..

