I use this in my .bashrc
file to automatically workon
the most recently activated virtual environment.
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
# Set up hooks to automatically enter last virtual env
export LAST_VENV_FILE=${WORKON_HOME}/.last_virtual_env
echo -e "#!/bin/bash\necho \$1 > $LAST_VENV_FILE" > $WORKON_HOME/preactivate
echo -e "#!/bin/bash\necho '' > $LAST_VENV_FILE" > $WORKON_HOME/predeactivate
chmod +x $WORKON_HOME/preactivate
chmod +x $WORKON_HOME/predeactivate
if [ -f $LAST_VENV_FILE ]; then
LAST_VENV=$(tail -n 1 $LAST_VENV_FILE)
if [ ! -z $LAST_VENV ]; then
# Automatically re-enter virtual environment
workon $LAST_VENV
fi
fi
fi
The preactivate
and deactivate
hooks are modified so that when a virtual environment is activated, the name of the virtual environment is dumped to a file, and when it's deactivated the contents of the file is erased. This is similar to @Todd Vanyo's answer, but works when activating/deactivating instead of when navigating directories.