python.plugin.zsh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Find python file
  2. alias pyfind='find . -name "*.py"'
  3. # Remove python compiled byte-code and mypy/pytest cache in either the current
  4. # directory or in a list of specified directories (including sub directories).
  5. function pyclean() {
  6. ZSH_PYCLEAN_PLACES=${*:-'.'}
  7. find ${ZSH_PYCLEAN_PLACES} -type f -name "*.py[co]" -delete
  8. find ${ZSH_PYCLEAN_PLACES} -type d -name "__pycache__" -delete
  9. find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
  10. find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
  11. }
  12. # Add the user installed site-packages paths to PYTHONPATH, only if the
  13. # directory exists. Also preserve the current PYTHONPATH value.
  14. # Feel free to autorun this when .zshrc loads.
  15. function pyuserpaths() {
  16. local targets=("python2" "python3") # bins
  17. # Get existing interpreters.
  18. local interps=()
  19. for target in $targets; do
  20. [ `command -v $target` ] && interps+=($target)
  21. done
  22. # Check for a non-standard install directory.
  23. local user_base="${HOME}/.local"
  24. [ $PYTHONUSERBASE ] && user_base=$PYTHONUSERBASE
  25. # Add version specific paths, if:
  26. # it exists in the filesystem;
  27. # it isn't in PYTHONPATH already.
  28. for interp in $interps; do
  29. # Get minor release version.
  30. local ver=`$interp -V 2>&1`
  31. ver=`echo ${ver:7} | cut -d '.' -f 1,2` # The patch version is variable length, truncate it.
  32. local site_pkgs="${user_base}/lib/python${ver}/site-packages"
  33. [[ -d $site_pkgs && ! $PYTHONPATH =~ $site_pkgs ]] && export PYTHONPATH=${site_pkgs}:$PYTHONPATH
  34. done
  35. }
  36. # Grep among .py files
  37. alias pygrep='grep -r --include="*.py"'
  38. # Run proper IPython regarding current virtualenv (if any)
  39. alias ipython="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"