Set Environment Variable for Virtualenv
March 2, 2016
Environment variables are a good way to set variables needed in your application, specifically Django on Heroku.
What is an environment variable? Environment variables are set for your current shell session and are used to pass information into system processes. I am over simplifying the definition here to keep it simple for this example.
In an *nix type system, from the command line, you can type printenv
to display your current environment variables.
You can also use the echo
command to display a single variable.
Example: echo $SHELL
to display your current shell application.
To set a variable from the command line use export MYVAR=value
In this article I use virtualenvwrapper for managing Python virtualenvs.
A great example of using an environment variable is setting a database password, or other confidential information. You wouldn’t store this private information in your settings file where it would publicly be visible, or saved in a software repository. You would store this information in an environment variable. When your application needs the database password, it would know to get the password from the ENV variable.
A great feature of virtualenv and virtualenvwrapper is the use of hooks.
The two hooks we are interested in for this example are the postactivate
hook and the postdeactivate
hook and they will be called when
activating and deactivating your virtualenv.
When you activate a virtualenv, you want to set your ENV variables for your project or app. When you deactivate your virtualenv you would unset the variables you set for your project.
Django app example
In this example I will be setting variables used in a Django app while using a virtaulenvwrapper.
First we will set our variables in the postactivate
hook. This is a
file similar to a .bash_profile
file where you set aliases etc.
Normally your virtualenvs are located at: ~/.virtualenvs
You can confirm this by checking: echo $VIRTUAL_ENV
Now you can locate your postactivate file and set your variables:
~/.virtualenv/[projectname]/bin/postactivate
#!/bin/bash
export DATABASE_NAME='yourdata'
export DATABASE_USER='yourdata'
export DATABASE_PASSWORD='yourprivatepassword'
# Add other private data like API keys.
export S3_KEY='yourkey'
export S3_SECRET='yoursecret'
# It is a good idea to store your Django secret key also.
export SECRET_KEY='youruniqueunpredictablevalue'
Now when you start your virtualenv, your Django app will have the above data
available and you will use this data in your setting.py
file. For
Django to have access to these variables you will need to import os
to
get access to system variables.
import os
"""
Django settings file for you project.
"""
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DATABASE_NAME'],
'USER': os.environ['DATABASE_USER'],
'PASSWORD': os.environ['DATABASE_PASSWORD'],
}
}
In your settings file above, you will see your environment variables you set and not the actual confidential data.
When you finish working on your project, you unset these variables with the
postdeactivate
hook.
~/.virtualenv/[projectname]/bin/postdeactivate
#!/bin/bash
unset DATABASE_NAME
unset DATABASE_USER
unset DATABASE_PASSWORD
Setting ENV variables for Heroku
You can edit your environment variables (config vars) in your application’s settings tab in your dashboard.
Then your setting will be available to your application.
You can also download and use the Heroku command line utility.
View, set, and edit your config vars with the simple commands provided.
heroku config *show all current settings*
heroku config:set MYVAR=myinfo
heroku config:get MYVAR
Environment variables are set for your current shell and running processes. Check your server configuration to determine if there are limitations for environment variables.