Wednesday, February 18, 2015

Load python virtual env on cd

I was wondering if there is way to automatically load a virtual env, if present, on `cd`ing to a directory.

This thought pestered me as I am so used to have a .rvmrc under a folder to control the version of ruby and gemset loaded, automatically when I cd to that folder.

This is what I came up with for virtual env.

Have this code in your .bash_profile/.zshrc (whatever you are using).

  `cat env/bin/activate > /dev/null 2>&1`
  if [ $? -eq 0 ]; then
    NC='\033[0m' # No Color]';
    echo -e "${green}Found a virtual environment!!! activating it.... deactivate yourself if you want${NC}";
    source env/bin/activate
function cd () { builtin cd "$@" && loadvenv; }

No comments:

Post a Comment