python.plugin.zsh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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()'"