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
}
@maxwellmckinnon

This comment has been minimized.

Copy link

@maxwellmckinnon maxwellmckinnon commented Jul 8, 2019

This is great thanks. You might want to add protection against rmvenv with no argument given, so that someone doesn't blow away all their environments :)

@dbtek

This comment has been minimized.

Copy link
Owner Author

@dbtek dbtek commented Jul 8, 2019

@maxwellmckinnon you are absolutely right. Any suggestions?

@dbtek

This comment has been minimized.

Copy link
Owner Author

@dbtek dbtek commented Jul 9, 2019

Updated gist with no argument checks. Thanks to maxwellmckinnon.

@Parth576

This comment has been minimized.

Copy link

@Parth576 Parth576 commented Jun 13, 2020

So glad I found this when I searched for "venv wrapper", works perfectly. Thanks!

@zekefarwell

This comment has been minimized.

Copy link

@zekefarwell zekefarwell commented Jul 29, 2020

Thanks for this! I found it handy to add an lsvenv command as well to list the existing virtual environments under ~/.venv/

lsvenv() {
  ls -1 $VENV_HOME
}
@dbtek

This comment has been minimized.

Copy link
Owner Author

@dbtek dbtek commented Aug 13, 2020

Thanks @zekefarwell. Added lsvenv.

@leoschmitz

This comment has been minimized.

Copy link

@leoschmitz leoschmitz commented Oct 1, 2020

Useful gist 👍 . I added complete -W "`lsvenv`" venv to the end of it so venv can autocomplete with the tab key. It might help you out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.