ruk·si

🐍 Python
venvs

Updated at 2019-08-03 08:59

Install virtualenv:

brew install python python3

# double check that it's not using system Python
which pip2
which pip3
which python2
which python3

pip3 install --upgrade pip
pip3 install virtualenv
virtualenv --version # check that it works

Add the following aliases to .bashrc.

v() {
    if [ ! -z "$VIRTUAL_ENV" ]; then
        deactivate
        if [ -z "$1" ]; then return 0; fi
    fi
    if [ -z "$1" ]; then
        echo "Usage: v <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    projectdir=~/Projects/$envname
    if [ ! -d "$venvdir" ]; then
        echo "Environment not found at $venvdir"
        return 2
    fi
    if [ ! -d "$projectdir" ]; then
        echo "Project directory not found at $projectdir"
        return 3
    fi
    source $venvdir/bin/activate
    cd $projectdir
    # silently try to activate right node version if .nvmrc is found
    nvm use > /dev/null 2> /dev/null
}

vmk2() {
    if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
    if [ -z "$1" ]; then
        echo "Usage: vmk2 <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    projectdir=~/Projects/$envname
    virtualenv -p `which python2` $venvdir
    if [ ! -d "$projectdir" ]; then mkdir $projectdir; fi
    echo "Created"
}

# Rebuild venv, they can get broken on Python upgrades
vfix2() {
    if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
    if [ -z "$1" ]; then
        echo "Usage: vfix2 <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    find $venvdir -type l -delete
    virtualenv -p `which python2` $venvdir
    echo "Fixed symlinks"
}

vmk3() {
    if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
    if [ -z "$1" ]; then
        echo "Usage: vmk3 <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    projectdir=~/Projects/$envname
    virtualenv -p `which python3` $venvdir
    if [ ! -d "$projectdir" ]; then mkdir $projectdir; fi
    echo "Created"
}

# Rebuild venv, they can get broken on Python upgrades
vfix3() {
    if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
    if [ -z "$1" ]; then
        echo "Usage: vfix3 <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    find $venvdir -type l -delete
    virtualenv -p `which python3` $venvdir
    echo "Fixed symlinks"
}

vrm() {
    if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
    if [ -z "$1" ]; then
        echo "Usage: vrm <environment name>"
        return 1
    fi
    envname=$1
    venvdir=~/venvs/$envname
    projectdir=~/Projects/$envname
    if [ -d "$venvdir" ]; then rm -r -f $venvdir; fi
    echo "Virtual environment removed, the project was left as is"
}

vls() {
    ls ~/venvs
}

# Adds tab completion to v and vrm commands.
# brew install findutils
_directories_in_build()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    VENVS=$(gfind ~/venvs -maxdepth 1 -type d -printf '%f\n')
    COMPREPLY=( $(compgen -W "${VENVS}" -- $cur) )
}
complete -F _directories_in_build v
complete -F _directories_in_build vrm
complete -F _directories_in_build vfix2
complete -F _directories_in_build vfix3

Usage:

vmk2 project-name   # create new Python 2 venv and project directory
vmk3 project-name   # create new Python 3 venv and project directory
v project-name      # activate target venv and cd to the project directory
v                   # deactivate current venv
vrm project-name    # remove target venv and project directory
vls                 # list all venvs

Sources