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,
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 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
These environmental variables need to be deactivated after use, which is done using the
(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.
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')