Skip to content

Instantly share code, notes, and snippets.

@jdvala
Last active February 8, 2020 13:41
Show Gist options
  • Save jdvala/cd9d1374f700bf5188139cc468d6770f to your computer and use it in GitHub Desktop.
Save jdvala/cd9d1374f700bf5188139cc468d6770f to your computer and use it in GitHub Desktop.
Manage python virtual environments

Custom function for python virtual environment

# create a new directory for all your virtual environments, preferably in your home folder
mkidr environments

Add the below function to your bashrc or zshrc

create_venv(){
        # check if folder exist
        if test -d ~/environments/"$1"; then
                echo "Environment already present, activating it"
                source ~/environments/"$1"/bin/activate
        else
                python3 -m venv ~/environments/"$1"
                echo "Activating environment"
                echo $1
                source ~/environments/"$1"/bin/activate 
        fi
}

Usage

create_venv my_new_env
# The above line will create a new environment and activate it if it doesnt exist already or create a new one and activate it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment