Browse files

This is a helper method to load virtualenvs quickly.

The function `use_env` is added to the autoenv shell script, so it is
sourced and available to be used inside `.env` scripts.  This makes it
easy to declare what virtualenv should be loaded or created when
switching to a specific dir.

For example:

    use_env myenv

The function is cheap when the environment is already active and will
only invoke the expensive `workon` and `mkvirtualenv` commands when
absolutely necessary.
  • Loading branch information...
1 parent 215ef3a commit 322e472591e03caabf4df8822d32b1a7c751307f @nvie committed Mar 29, 2012
Showing with 20 additions and 0 deletions.
  1. +20 −0 activate.sh
View
20 activate.sh
@@ -117,3 +117,23 @@ autoenv_cd()
cd() {
autoenv_cd "$@"
}
+
+
+# The use_env call below is a reusable command to activate/create a new Python
+# virtualenv, requiring only a single declarative line of code in your .env files.
+# It only performs an action if the requested virtualenv is not the current one.
+use_env() {
+ typeset venv
+ venv="$1"
+ if [[ "${VIRTUAL_ENV:t}" != "$venv" ]]; then
+ if workon | grep -q "$venv"; then
+ workon "$venv"
+ else
+ echo -n "Create virtualenv \"$venv\" now? (Yn) "
+ read answer
+ if [[ "$answer" != "n" ]]; then
+ mkvirtualenv "$venv"
+ fi
+ fi
+ fi
+}

0 comments on commit 322e472

Please sign in to comment.