Skip to content

Instantly share code, notes, and snippets.

@mwcraig
Forked from lexicalunit/conda virtualenv bashrc
Created June 12, 2014 19:05
Show Gist options
  • Save mwcraig/cc0254ee4992ff55e6bc to your computer and use it in GitHub Desktop.
Save mwcraig/cc0254ee4992ff55e6bc to your computer and use it in GitHub Desktop.
# .bashrc
################################################################################
# python environment control
################################################################################
export PYTHON_ENV=""
function entervirtualenv
{
if type virtualenvwrapper.sh >/dev/null 2>&1; then
source "$(which virtualenvwrapper.sh)"
fi
export PYTHON_ENV="virtualenv"
}
function exitvirtualenv
{
if [[ -n "$VIRTUAL_ENV" ]]; then
if type deactivate >/dev/null 2>&1; then
deactivate
fi
fi
unset PYTHON_ENV
}
function enterconda
{
# assume anaconda is installed in your home directory unless specified
if [[ -z "$1" ]]; then
export ANACONDA_ROOT="$HOME/anaconda"
else
export ANACONDA_ROOT="$1"
fi
[[ -d "$ANACONDA_ROOT/bin" ]] && PATH="$ANACONDA_ROOT/bin:$PATH"
[[ -d "$ANACONDA_ROOT/share/man" ]] && MANPATH="$ANACONDA_ROOT/share/man:$MANPATH"
export PATH
export MANPATH
export PYTHON_ENV="conda"
}
function exitconda
{
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
if which deactivate >/dev/null 2>&1; then
source deactivate
fi
fi
export PATH="$(echo "$PATH" | sed "s@$ANACONDA_ROOT/bin@@g;s@::@:@g;s@^:@@;s@:\$@@;")"
export MANPATH="$(echo "$PATH" | sed "s@$ANACONDA_ROOT/share/man@@g;s@::@:@g;s@^:@@;s@:\$@@;")"
unset PYTHON_ENV
}
################################################################################
# setup command prompt
################################################################################
function _setup_command_prompt
{
local DISPLAY_PYTHON_ENV=""
if [[ -n "$PYTHON_ENV" ]]; then
DISPLAY_PYTHON_ENV="$PYTHON_ENV"
if [[ -n "$VIRTUAL_ENV" ]]; then
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV:`basename \"$VIRTUAL_ENV\"`"
elif [[ -n "$CONDA_DEFAULT_ENV" ]]; then
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV:$CONDA_DEFAULT_ENV"
fi
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV"
fi
export PS1=... # Put $DISPLAY_PYTHON_ENV into your PS1 however you want to display it.
}
export PROMPT_COMMAND='_setup_command_prompt ' # single quotes to avoid variable expansion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment