python.plugin.zsh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # python command
  2. alias py='python'
  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. ZSH_PYCLEAN_PLACES=${*:-'.'}
  9. find ${ZSH_PYCLEAN_PLACES} -type f -name "*.py[co]" -delete
  10. find ${ZSH_PYCLEAN_PLACES} -type d -name "__pycache__" -delete
  11. find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
  12. find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
  13. }
  14. # Add the user installed site-packages paths to PYTHONPATH, only if the
  15. # directory exists. Also preserve the current PYTHONPATH value.
  16. # Feel free to autorun this when .zshrc loads.
  17. function pyuserpaths() {
  18. local targets=("python2" "python3") # bins
  19. # Get existing interpreters.
  20. local interps=()
  21. for target in $targets; do
  22. [ `command -v $target` ] && interps+=($target)
  23. done
  24. # Check for a non-standard install directory.
  25. local user_base="${HOME}/.local"
  26. [ $PYTHONUSERBASE ] && user_base=$PYTHONUSERBASE
  27. # Add version specific paths, if:
  28. # it exists in the filesystem;
  29. # it isn't in PYTHONPATH already.
  30. for interp in $interps; do
  31. # Get minor release version.
  32. local ver=`$interp -V 2>&1`
  33. ver=`echo ${ver:7} | cut -d '.' -f 1,2` # The patch version is variable length, truncate it.
  34. local site_pkgs="${user_base}/lib/python${ver}/site-packages"
  35. [[ -d $site_pkgs && ! $PYTHONPATH =~ $site_pkgs ]] && export PYTHONPATH=${site_pkgs}:$PYTHONPATH
  36. done
  37. }
  38. # Grep among .py files
  39. alias pygrep='grep -nr --include="*.py"'
  40. # Run proper IPython regarding current virtualenv (if any)
  41. alias ipython="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"
  42. # Share local directory as a HTTP server
  43. alias pyserver="python -m http.server"
  44. ## venv utilities
  45. # Activate a the python virtual environment specified.
  46. # If none specified, use 'venv'.
  47. function vrun() {
  48. local name="${1:-venv}"
  49. local venvpath="${name:P}"
  50. if [[ ! -d "$venvpath" ]]; then
  51. echo >&2 "Error: no such venv in current directory: $name"
  52. return 1
  53. fi
  54. if [[ ! -f "${venvpath}/bin/activate" ]]; then
  55. echo >&2 "Error: '${name}' is not a proper virtual environment"
  56. return 1
  57. fi
  58. . "${venvpath}/bin/activate" || return $?
  59. echo "Activated virtual environment ${name}"
  60. }
  61. # Create a new virtual environment, with default name 'venv'.
  62. function mkv() {
  63. local name="${1:-venv}"
  64. local venvpath="${name:P}"
  65. python3 -m venv "${name}" || return
  66. echo >&2 "Created venv in '${venvpath}'"
  67. vrun "${name}"
  68. }