Bash helper functions for venv

L.J. Hanson - - 1 min read

Working with python, I’ve always utilized virtualenvwrapper for creating and managing python setups for developing. Now that more and more python work is moving over to Python 3, I wanted to start using more of the functionality it brings natively. One of these is Venv which is the native virtual environment system.

One thing missing from the builtin system is some nice helper functions for use on the command line. The following were copied from various1 sources2 but are posted below for reference as they should allow for the most common use cases.

# Python venv support functions
export VENV_HOME="$HOME/.venv"
[[ -d $VENV_HOME ]] || mkdir $VENV_HOME

venv() {
  source "$VENV_HOME/$1/bin/activate"
}

mkvenv() {
  python3 -m venv $VENV_HOME/$1
}

rmvenv() {
  rm -r $VENV_HOME/$1
}

lsvenv(){
    ls $VENV_HOME
}