• Home
  • About
    • Richie's Blog photo

      Richie's Blog

      Science, data, biology, digital health, programming, tech

    • Learn More
    • Twitter
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Setting and using environmental variables using virtualenvwrapper

24 May 2016

Reading time ~1 minute

Environmental variables are a great way to dynamically set credentials and other variables, which helps to increase portability of projects between systems. The excellent virtualenvwrapper, which helps when setting up and using python virtual environments, makes setting these environmental variables a trivial process.

Inside the bin directory of each virtualenv, along with installed modules will be four configuration files, preactivate, postactivate, predeactivate and postdeactivate. These files are hooks, which run automatically every time an event is run in the virtualenv. E.g. the preactivate hook is run each time the virtualenv is set up, running before activation. The location of your virtualenv is set during installation, and can be easily found by either echoing the $WORKON_HOME variable, or else activating a virtualenv and echoing its $VIRTUAL_ENV variable. The hooks are all found inside the bin directory of the virtualenv, along with other file including any installed python modules.

-- Find the root of all virtualenv, set during installation
$ echo $WORKON_HOME
/Users/johndoe/.virtualenvs

-- Alternatively, activate your virtualenv & find location of the individual virtualenv
$ workon fooenv
(fooenv) $ echo $VIRTUAL_ENV
/Users/johndoe/.virtualenvs/fooenv

-- List files in bin directory
(fooenv) $ ls $VIRTUAL_ENV/bin/
... (other files, including installed python modules) ...
postactivate
postdeactivate
preactivate
predeactivate
... (other files, including installed python modules) ... 

Setting variables

Setting variables is done in the postactivate hook, where foo_user/foo_pass etc would be replaced by appropriate mysql username and password.

(fooenv)$ vi $VIRTUAL_ENV/bin/postactivate

#!/bin/bash
# This hook is run after this virtualenv is activated.
export MYSQL_USER=foo_user
export MYSQL_PASS=foo_pass
export S3_KEY=foo_s3_key
export S3_SECRET=foo_s3_secret

Unsetting variables

These environmental variables need to be deactivated after use, which is done using the predeactivate hook.

(fooenv)$ vi $VIRTUAL_ENV/bin/predeactivate

#!/bin/bash
# This hook is run before this virtualenv is deactivated.
unset MYSQL_USER
unset MYSQL_PASS
unset S3_KEY
unset S3_SECRET

NB Unsetting these variables will unset all systemwide variabes of the same name, so care must be taken in this case. Further reading can be found at the excellent StackOverflow answer by Danilo Bargen http://stackoverflow.com/questions/9554087/setting-an-environment-variable-in-virtualenv.

Using variables

Environmental variables can be called from the command line by calling echo

(fooenv)$ echo $MYSQL_PASS
foo_pass

More usefully, they can be called inside python, using os.environ.

import os
import MySQLdb as mdb

mysql_user = os.environ['MYSQL_USER']
mysql_pass = os.environ['MYSQL_PASS']

# Initialise mysql connection
con = mdb.connect('localhost', mysql_user, mysql_pass, 'some_db')


Like Tweet +1