# Automatic virtualenv activation

### Demonstration

Before, I had to do something like:

    cd myproject
source ../env/bin/activate

Now I just do:

cd myproject


### Virtualenv standard

Say I have a project called projectX, you could expect to find it as such on my servers:

    /srv
/projectX_prod
/projectX_test
/projectX_dev
/projectX_dev_env -> the virtualenv
/main -> the checkout of the python project

As you can see, it is easy and consistent. For the record, this is the command I use to create the symlink:

    cd /srv/projectX_prod
ln -sfn projectX_dev_env env

### Shell hack

Usually, when I want to hack a project, it starts with:

cd /srv/projectX_dev


Or even:

cd /srv/projectX_dev/main


I know that this is always how it will start. So I added a hack in my shell init script to enable the corresponding virtualenv automatically. It is pretty self explanatory, so you should be able to hack it easily:

    function cd() {
builtin cd "\$@";
if [ -f env/bin/activate ]
then
source env/bin/activate
elif [ -f bin/activate ]
then
source bin/activate
elif [ -f ../env/bin/activate ]
then
source ../env/bin/activate
fi
}