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

With Virtualenv:

This assumes that you already know how to set up a Virtualenv and a Django project.

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:

/home/username/project/
├── bin
├── include
├── lib
├── pip-selfcheck.json
└── undefined
4 directories, 1 file

pip-selfcheck.json is the only file. All the rest are directories.

In the bin directory edit the activate script:

/home/grenouille/project/
├── bin
│ ├── activate

There should be a definition for deactivate in the activate script. Add the following: [1]

deactivate() {
...
    # Unset variables
unset SECRET_KEY
}

What the above does is unset your environment variables once the virtual environment is deactivated.

At the end of the script, set your variables

export SECRET_KEY="a5k6k44YiOPG)"

Deactivate and reactivate your virtual environment for your changes to take effect. [2]

To deactivate run:

$ deactivate

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

Then

$ python manage.py shell

In the shell run:

>>> import os
>>> os.environ.get('SECRET_KEY')*

This should output the environment variable you saved earlier

os.environ is a unix builtin that looksup the value of the given environment variable. [3]

Without Virtualenv:

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

export SECRET_KEY="a5k6k44YiOPG)"

Save and exit the script by pressing

:wq

and run

$ source .bashrc

Running source makes the changes take effect by executing the shell script in the current context. [4]

[1] — https://stackoverflow.com/a/38645983

[2] — https://virtualenv.pypa.io/en/stable/userguide/#activate-script

[3] — https://docs.python.org/3/library/os.html#os.environ

[4] — https://askubuntu.com/a/107975

[4] — https://unix.stackexchange.com/a/17885

*-edit based on a recommendation by Incognos

Show your support

Clapping shows how much you appreciated Daniel Gitu’s story.