init.zsh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #
  2. # Enables local Python package installation.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. # Sebastian Wiesner <lunaryorn@googlemail.com>
  7. # Stefano Mazzucco <stefano@curso.re>
  8. # Patrick Bos <egpbos@gmail.com>
  9. # Indrajit Raychaudhuri <irc@indrajit.com>
  10. #
  11. #
  12. # Options
  13. #
  14. setopt EXTENDED_GLOB
  15. # Load dependencies.
  16. pmodload 'helper'
  17. # Return if requirements are not found.
  18. if (( ! $+commands[python] )); then
  19. return 1
  20. fi
  21. # Export PYTHONSTARTUP if it exists.
  22. if [[ -z "$PYTHONSTARTUP" ]]; then
  23. if [[ -f "~/.python_startup.py" ]]; then
  24. export PYTHONSTARTUP=~/.python_startup.py
  25. fi
  26. fi
  27. # Load manually installed pyenv into the path
  28. if [[ -s "${PYENV_ROOT:=$HOME/.pyenv}/bin/pyenv" ]]; then
  29. path=("${PYENV_ROOT}/bin" $path)
  30. eval "$(pyenv init - --no-rehash zsh)"
  31. # Ensure manually installed pyenv is added to path when present.
  32. [[ -s $local_pyenv ]] && path=($local_pyenv:h $path)
  33. # Load pyenv into the shell session.
  34. eval "$(pyenv init - zsh)"
  35. # Prepend PEP 370 per user site packages directory, which defaults to
  36. # ~/Library/Python on macOS and ~/.local elsewhere, to PATH. The
  37. # path can be overridden using PYTHONUSERBASE.
  38. else
  39. if [[ -n "$PYTHONUSERBASE" ]]; then
  40. path=($PYTHONUSERBASE/bin(N) $path)
  41. elif is-darwin; then
  42. path=($HOME/Library/Python/*/bin(N) $path)
  43. else
  44. # This is subject to change.
  45. path=($HOME/.local/bin(N) $path)
  46. fi
  47. fi
  48. unset local_pyenv
  49. # Return if requirements are not found.
  50. if (( ! $+commands[(i)python[0-9.]#] && ! $+functions[pyenv] && ! $+commands[conda] )); then
  51. return 1
  52. fi
  53. function _python-workon-cwd {
  54. # Check if this is a Git repo.
  55. local GIT_REPO_ROOT="$(git rev-parse --show-toplevel 2> /dev/null)"
  56. # Get absolute path, resolving symlinks.
  57. local PROJECT_ROOT="$PWD:A"
  58. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  59. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  60. PROJECT_ROOT="$PROJECT_ROOT:h"
  61. done
  62. if [[ $PROJECT_ROOT == "/" ]]; then
  63. PROJECT_ROOT="."
  64. fi
  65. # Check for virtualenv name override.
  66. local ENV_NAME=""
  67. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  68. ENV_NAME="$(<$PROJECT_ROOT/.venv)"
  69. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]]; then
  70. ENV_NAME="$PROJECT_ROOT/.venv"
  71. elif [[ $PROJECT_ROOT != "." ]]; then
  72. ENV_NAME="$PROJECT_ROOT:t"
  73. fi
  74. if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
  75. # We've just left the repo, deactivate the environment.
  76. # Note: this only happens if the virtualenv was activated automatically.
  77. deactivate && unset CD_VIRTUAL_ENV
  78. fi
  79. if [[ $ENV_NAME != "" ]]; then
  80. # Activate the environment only if it is not already active.
  81. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  82. if [[ -n "$WORKON_HOME" && -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  83. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  84. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  85. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  86. fi
  87. fi
  88. fi
  89. }
  90. # Load auto workon cwd hook.
  91. if zstyle -t ':prezto:module:python:virtualenv' auto-switch; then
  92. # Auto workon when changing directory.
  93. autoload -Uz add-zsh-hook
  94. add-zsh-hook chpwd _python-workon-cwd
  95. fi
  96. # Load virtualenvwrapper into the shell session, if pre-requisites are met
  97. # and unless explicitly requested not to
  98. if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) \
  99. && zstyle -T ':prezto:module:python:virtualenv' initialize ; then
  100. # Set the directory where virtual environments are stored.
  101. export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}"
  102. [[ -d $WORKON_HOME ]] || mkdir $WORKON_HOME
  103. # Disable the virtualenv prompt. Note that we use the magic value used by the
  104. # pure prompt because there's some additional logic in that prompt which tries
  105. # to figure out if a user set this variable and disable the python portion of
  106. # that prompt based on it which is the exact opposite of what we want to do.
  107. export VIRTUAL_ENV_DISABLE_PROMPT=12
  108. # Create a sorted array of available virtualenv related 'pyenv' commands to
  109. # look for plugins of interest. Scanning shell '$path' isn't enough as they
  110. # can exist in 'pyenv' synthesized paths (e.g., '~/.pyenv/plugins') instead.
  111. local -a pyenv_plugins
  112. local pyenv_virtualenvwrapper_plugin_found
  113. if (( $+commands[pyenv] )); then
  114. pyenv_plugins=(${(@oM)${(f)"$(pyenv commands --no-sh 2> /dev/null)"}:#virtualenv*})
  115. # Optionally activate 'virtualenv-init' plugin when available.
  116. if (( $pyenv_plugins[(i)virtualenv-init] <= $#pyenv_plugins )); then
  117. eval "$(pyenv virtualenv-init - zsh)"
  118. fi
  119. # Optionally activate 'virtualenvwrapper' plugin when available.
  120. if (( $pyenv_plugins[(i)virtualenvwrapper(_lazy|)] <= $#pyenv_plugins )); then
  121. pyenv "$pyenv_plugins[(R)virtualenvwrapper(_lazy|)]"
  122. pyenv_virtualenvwrapper_plugin_found="true"
  123. fi
  124. unset pyenv_plugins
  125. fi
  126. if [[ $pyenv_virtualenvwrapper_plugin_found != "true" ]]; then
  127. # Fallback to standard 'virtualenvwrapper' if 'python' is available in '$path'.
  128. if (( ! $+VIRTUALENVWRAPPER_PYTHON )) && (( $+commands[(i)python[0-9.]#] )); then
  129. VIRTUALENVWRAPPER_PYTHON=$commands[(i)python[0-9.]#]
  130. fi
  131. virtualenvwrapper_sources=(
  132. ${(@Ov)commands[(I)virtualenvwrapper(_lazy|).sh]}
  133. /usr/share/virtualenvwrapper/virtualenvwrapper(_lazy|).sh(OnN)
  134. )
  135. if (( $#virtualenvwrapper_sources )); then
  136. source "$virtualenvwrapper_sources[1]"
  137. fi
  138. unset virtualenvwrapper_sources
  139. fi
  140. unset pyenv_virtualenvwrapper_plugin_found
  141. fi
  142. # Load conda into the shell session, if requested.
  143. zstyle -T ':prezto:module:python' conda-init
  144. if (( $? && $+commands[conda] )); then
  145. if (( $(conda ..changeps1) )); then
  146. echo "To make sure Conda doesn't change your prompt (should do that in the prompt module) run:\n conda config --set changeps1 false"
  147. # TODO:
  148. # We could just run this ourselves. In an exit hook
  149. # (add zsh-hook zshexit [(anonymous) function]) we could then set it back
  150. # to the way it was before we changed it. However, I'm not sure if this is
  151. # exception safe, so left it like this for now.
  152. fi
  153. fi
  154. #
  155. # Aliases
  156. #
  157. if ! zstyle -t ':prezto:module:python:alias' skip; then
  158. alias py='python'
  159. alias py2='python2'
  160. alias py3='python3'
  161. fi