There was this question today. The current answer does not take into account the fact that the environment is activated every time you cd to a subfolder or back to the root folder. Solved it with a .env script:
venv=venv currentvenv="" if [[ $VIRTUAL_ENV != "" ]] then # Strip out the path and just leave the env name currentvenv="${VIRTUAL_ENV##*/}" fi if [[ "$currentvenv" != "$venv" ]] then echo "Switching to environment: $venv" workon $venv #else # echo "Already on environment $venv" fi
Replace venv name of your environment. You can uncomment the else block to make sure that it does not try to activate the environment each time, given that the desired environment is already activated.
Note. If you are not using virtualenvwrapper , you must replace the workon command workon any command that you use to activate your virtual environment. I recommend using virtualenvwrapper though.
bjorgvin
source share