Advertisement
wrequiems

Untitled

May 10th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. # Finds a Python virtual environment in the current
  2. # directory or any of its parents and enters it, or
  3. # exits an virtual environment if we're in one
  4. # but the current directory does not contain a virtualenv
  5. function find_and_enter_virtualenv {
  6. # See if the current directory, or any of its
  7. # parents have a 'venv/bin/activate' script
  8. path=$(pwd)
  9. while [ ! -z $path ]; do
  10. if [ -f "$path/venv/bin/activate" ]; then
  11. path="$path/venv/bin/activate"
  12. break
  13. fi
  14.  
  15. path="${path%/*}"
  16. done
  17.  
  18. # No virtualenv found, try to de-activate
  19. # in case we're exiting one
  20. if [ -z $path ]; then
  21. deactivate &> /dev/null
  22. return
  23. fi
  24.  
  25. # Enter the virtualenv
  26. source $path
  27.  
  28. # Fix up the command prompt to make it
  29. # clear we're in a virtual env
  30. export PS1="\n$REDBOLD\u $CYAN(venv)$RESETCOLOR $PURPLE@ $GREEN\w $RESETCOLOR$CYAN → $RESETCOLOR"
  31. }
  32.  
  33. # Override cd so we can intercept directory changes
  34. function cd {
  35. builtin cd "$@"
  36. find_and_enter_virtualenv
  37. }
  38.  
  39. # Suppose a bash prompt is opened and that directory
  40. # is a virtual env, we'll activate it!
  41. find_and_enter_virtualenv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement