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:

├── 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:

├── bin
│ ├── activate

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

deactivate() {
# 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

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


$ python 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


and run

$ source .bashrc

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

[1] —

[2] —

[3] —

[4] —

[4] —

*-edit based on a recommendation by Incognos

I am a neophyte. I like challenging myself and learning new things. Currently learning python, among other things

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store