python.plugin.zsh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. auto_vrun() {
  71. if [[ -f "${PYTHON_VENV_NAME}/bin/activate" ]]; then
  72. source "${PYTHON_VENV_NAME}/bin/activate" > /dev/null 2>&1
  73. else
  74. (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
  75. fi
  76. }
  77. add-zsh-hook chpwd auto_vrun
  78. auto_vrun
  79. fi