The Environment Variables Pattern
So you’ve got your project that consumes various APIs and has secret keys.
You always hear how you’re supposed to keep them outside version control but the articles you find online are made out to be more complicated than they should.
This guide will show you how to use environment variables to store your secret keys, API keys and database keys outside of version control when you have Virtualenv enabled and when you don’t.
Note: This is based on my Django use case while running ubuntu 16.04
Let’s take for example that your project lives in a virtual environment called project that has a project called undefined.
Your virtual environment has the following structure:
4 directories, 1 file
pip-selfcheck.json is the only file. All the rest are directories.
In the bin directory edit the activate script:
│ ├── activate
There should be a definition for deactivate in the activate script. Add the following: 
# Unset variables
What the above does is unset your environment variables once the virtual environment is deactivated.
At the end of the script, set your variables
Deactivate and reactivate your virtual environment for your changes to take effect. 
To deactivate run:
To activate run”
$ source bin/activate
Next ensure that your variables are accessible in your project.
In your project start a new python shell
$ cd undefined
$ python manage.py shell
In the shell run:
>>> import os
This should output the environment variable you saved earlier
os.environ is a unix builtin that looksup the value of the given environment variable. 
If you do not have a virtual env but still want to have environment variables add the line below to the end of either the .bashrc, the .bash_profile or the .profile scripts. Pick one of the files to add the line to.
For illustration purposes lets pick the .bashrc
To edit the file use a terminal editor like vim, nano, gedit etc.
I personally prefer vim.
$ vim .bashrc
This will open the script in the terminal.
Navigate to the end of the file and press i to edit and add the line
Save and exit the script by pressing
$ source .bashrc
Running source makes the changes take effect by executing the shell script in the current context. 
*-edit based on a recommendation by Incognos