Skip to content

Instantly share code, notes, and snippets.

@xtream1101
Last active June 30, 2016 18:53
Show Gist options
  • Save xtream1101/de110b8ee1c14aee9940637293a75781 to your computer and use it in GitHub Desktop.
Save xtream1101/de110b8ee1c14aee9940637293a75781 to your computer and use it in GitHub Desktop.
Python virtual envs bash commands
# Add this to your .bashrc or .bash_profile file to use
#
# This gives you access to create and switch between different python envs
# To create a venv called 'foo' just type:
# `pyve foo`
# This will create a python 3 venv and activate it as well
# If you are in a different venv and want to switch to foo, enter the same thing
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