Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Finds a Python virtual environment in the current
- # directory or any of its parents and enters it, or
- # exits an virtual environment if we're in one
- # but the current directory does not contain a virtualenv
- function find_and_enter_virtualenv {
- # See if the current directory, or any of its
- # parents have a 'venv/bin/activate' script
- path=$(pwd)
- while [ ! -z $path ]; do
- if [ -f "$path/venv/bin/activate" ]; then
- path="$path/venv/bin/activate"
- break
- fi
- path="${path%/*}"
- done
- # No virtualenv found, try to de-activate
- # in case we're exiting one
- if [ -z $path ]; then
- deactivate &> /dev/null
- return
- fi
- # Enter the virtualenv
- source $path
- # Fix up the command prompt to make it
- # clear we're in a virtual env
- export PS1="\n$REDBOLD\u $CYAN(venv)$RESETCOLOR $PURPLE@ $GREEN\w $RESETCOLOR$CYAN → $RESETCOLOR"
- }
- # Override cd so we can intercept directory changes
- function cd {
- builtin cd "$@"
- find_and_enter_virtualenv
- }
- # Suppose a bash prompt is opened and that directory
- # is a virtual env, we'll activate it!
- find_and_enter_virtualenv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement