Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python 3 venv wrapper. Manages all virtual environments under ~/.venv/ .
# include following in .bashrc / .bash_profile / .zshrc
# usage
# $ mkvenv myvirtualenv # creates venv under ~/.venv/
# $ venv myvirtualenv # activates venv
# $ deactivate # deactivates venv
# $ rmvenv myvirtualenv # removes venv
export VENV_HOME="$HOME/.venv"
[[ -d $VENV_HOME ]] || mkdir $VENV_HOME
lsvenv() {
ls -1 $VENV_HOME
}
venv() {
if [ $# -eq 0 ]
then
echo "Please provide venv name"
else
source "$VENV_HOME/$1/bin/activate"
fi
}
mkvenv() {
if [ $# -eq 0 ]
then
echo "Please provide venv name"
else
python3 -m venv $VENV_HOME/$1
fi
}
rmvenv() {
if [ $# -eq 0 ]
then
echo "Please provide venv name"
else
rm -r $VENV_HOME/$1
fi
}
@kuirolo
Copy link

kuirolo commented Mar 29, 2022

This is great! My system version is 3.10 which tends to break things, so I added a few lines so mkvenv can optionally set the version. It's a bit fragile and basic, but it works for my purposes.

mkvenv() {
  if [ $# -eq 0 ]
    then
      echo "Please provide venv name"
  elif [ $# -eq 1 ]
    then
      python3 -m venv $VENV_HOME/$1
  elif [ $# -eq 2 ]
    then
      python$2 -m venv $VENV_HOME/$1
  fi      
}

@billyzs
Copy link

billyzs commented Sep 23, 2022

the ZSH equivalent for @leoschmitz 's one liner above is
compdef '_path_files -/ -W $VENV_HOME' venv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment