3

Autoenv is super! But, my terminal has gotten a bit slower because autoenv calls workon every time I cd from one directory to another beneath the autoenv root. How can I customize my .env file so that workon is only called when I initially enter the directory?

user220419
  • 549
  • 1
  • 4
  • 11

2 Answers2

2

I have this in my .env file:

type deactivate &>/dev/null || source venv/bin/activate

Check if the environment is activated. If not, activate the environment otherwise do nothing.

N. Mauchle
  • 494
  • 1
  • 7
  • 20
1

If you have multiple projects, each with its own virtualenv, and you want to switch automatically between virtualenvs when you cd into the project folders (and of course make sure workon is only called when necessary) then you'll need some more logic in your .env file:

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

See https://stackoverflow.com/a/27987379/2915101

Community
  • 1
  • 1
bjorgvin
  • 186
  • 2
  • 5