python.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # set python command if 'py' not installed
  2. builtin which py > /dev/null || alias py='python3'
  3. # Find python file
  4. alias pyfind='find . -name "*.py"'
  5. # Remove python compiled byte-code and mypy/pytest cache in either the current
  6. # directory or in a list of specified directories (including sub directories).
  7. function pyclean() {
  8. find "${@:-.}" -type f -name "*.py[co]" -delete
  9. find "${@:-.}" -type d -name "__pycache__" -delete
  10. find "${@:-.}" -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
  11. find "${@:-.}" -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
  12. }
  13. # Add the user installed site-packages paths to PYTHONPATH, only if the
  14. # directory exists. Also preserve the current PYTHONPATH value.
  15. # Feel free to autorun this when .zshrc loads.
  16. function pyuserpaths() {
  17. setopt localoptions extendedglob
  18. # Check for a non-standard install directory.
  19. local user_base="${PYTHONUSERBASE:-"${HOME}/.local"}"
  20. local python version site_pkgs
  21. for python in python2 python3; do
  22. # Check if command exists
  23. (( ${+commands[$python]} )) || continue
  24. # Get minor release version.
  25. # The patch version is variable length, truncate it.
  26. version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
  27. # Add version specific path, if:
  28. # - it exists in the filesystem
  29. # - it isn't in $PYTHONPATH already.
  30. site_pkgs="${user_base}/lib/python${version}/site-packages"
  31. [[ -d "$site_pkgs" && ! "$PYTHONPATH" =~ (^|:)"$site_pkgs"(:|$) ]] || continue
  32. export PYTHONPATH="${site_pkgs}${PYTHONPATH+":${PYTHONPATH}"}"
  33. done
  34. }
  35. # Grep among .py files
  36. alias pygrep='grep -nr --include="*.py"'
  37. # Run proper IPython regarding current virtualenv (if any)
  38. alias ipython='python3 -c "import IPython, sys; sys.exit(IPython.start_ipython())"'
  39. # Share local directory as a HTTP server
  40. alias pyserver="python3 -m http.server"
  41. ## venv utilities
  42. : ${PYTHON_VENV_NAME:=venv}
  43. # Activate a the python virtual environment specified.
  44. # If none specified, use $PYTHON_VENV_NAME, else 'venv'.
  45. function vrun() {
  46. local name="${1:-$PYTHON_VENV_NAME}"
  47. local venvpath="${name:P}"
  48. if [[ ! -d "$venvpath" ]]; then
  49. echo >&2 "Error: no such venv in current directory: $name"
  50. return 1
  51. fi
  52. if [[ ! -f "${venvpath}/bin/activate" ]]; then
  53. echo >&2 "Error: '${name}' is not a proper virtual environment"
  54. return 1
  55. fi
  56. . "${venvpath}/bin/activate" || return $?
  57. echo "Activated virtual environment ${name}"
  58. }
  59. # Create a new virtual environment using the specified name.
  60. # If none specfied, use $PYTHON_VENV_NAME
  61. function mkv() {
  62. local name="${1:-$PYTHON_VENV_NAME}"
  63. local venvpath="${name:P}"
  64. python3 -m venv "${name}" || return
  65. echo >&2 "Created venv in '${venvpath}'"
  66. vrun "${name}"
  67. }
  68. if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
  69. # Automatically activate venv when changing dir
  70. function auto_vrun() {
  71. # deactivate if we're on a different dir than VIRTUAL_ENV states
  72. # we don't deactivate subdirectories!
  73. if (( $+functions[deactivate] )) && [[ $PWD != ${VIRTUAL_ENV:h}* ]]; then
  74. deactivate > /dev/null 2>&1
  75. fi
  76. if [[ $PWD != ${VIRTUAL_ENV:h} ]]; then
  77. for _file in "${PYTHON_VENV_NAME}"*/bin/activate(N.); do
  78. # make sure we're not in a venv already
  79. (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
  80. source $_file > /dev/null 2>&1
  81. break
  82. done
  83. fi
  84. }
  85. add-zsh-hook chpwd auto_vrun
  86. auto_vrun
  87. fi