Skip to content

Instantly share code, notes, and snippets.

@xtream1101
Last active December 6, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtream1101/039145614e3a2353559d39d6a7cf6611 to your computer and use it in GitHub Desktop.
Save xtream1101/039145614e3a2353559d39d6a7cf6611 to your computer and use it in GitHub Desktop.
Easy python environments
# Put this code in your .bashrc or .bash_profile
# Get Virtual Env
Color_Off="\033[0m" # Text Reset
Purple="\033[0;35m" # Purple
virtualenv_prompt() {
if [[ $VIRTUAL_ENV != "" ]]; then
venv=${VIRTUAL_ENV##*/}
# Strip out the path and just leave the env name
echo -e "($Purple$venv$Color_Off)"
fi
}
# Put this in the PS1 var: \$(virtualenv_prompt)
# Can only use pip in a virtualenv
export PIP_REQUIRE_VIRTUALENV=true
pyve(){
env_name=$1
activate_file=~/Virtualenvs/py3/$env_name/bin/activate
if [[ ! -a $activate_file ]]; then
# If a venv does not exist with that name then create it
virtualenv -p python3 ~/Virtualenvs/py3/$env_name
fi
# Switch to a python3 virtual env, deactivate the current one if there is
source $activate_file
}
_pyve(){
# Tab completion for pyve with the available envs that can be activated
local cur=${COMP_WORDS[COMP_CWORD]}
envs=`ls ~/Virtualenvs/py3/`
COMPREPLY=( $(compgen -W "$envs" -- $cur) )
}
# Associate the tab completion
complete -F _pyve pyve
pyve2(){
env_name=$1
activate_file=~/Virtualenvs/py2/$env_name/bin/activate
if [[ ! -a $activate_file ]]; then
# If a venv does not exist with that name then create it
virtualenv -p python2 ~/Virtualenvs/py2/$env_name
fi
# Switch to a python3 virtual env, deactivate the current one if there is
source $activate_file
}
_pyve2(){
# Tab completion for pyve with the available envs that can be activated
local cur=${COMP_WORDS[COMP_CWORD]}
envs=`ls ~/Virtualenvs/py2/`
COMPREPLY=( $(compgen -W "$envs" -- $cur) )
}
# Associate the tab completion
complete -F _pyve2 pyve2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment