From 19435b16eaf9af357da0bf547ded114f798d87ae Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Mon, 24 Jul 2017 19:34:11 -0500 Subject: [PATCH] [python] Improve and document `virtualenvwrapper` initialization flow Changes: * Simplify zstyle name `skip-virtualenvwrapper-init` to `initialize` avoiding double negation in name * Always perform `eval (pyenv virtualenv-init -)` at initialization * Prefer `virtualenvwrapper_lazy` over `virtualenvwrapper` when available * Honor `VIRTUALENVWRAPPER_VIRTUALENV` if it is defined. * Document about `VIRTUALENVWRAPPER_PYTHON` and `VIRTUALENVWRAPPER_VIRTUALENV` (this would be particularly important in macOS after recent homebrew update) * Add additional documentation for `initialize` in _README.md_ and _zpreztorc_ * Add aliases `py2`, `py3` as shortcut for `python2`, `python3` respectively --- modules/python/README.md | 24 +++++++++++++++++++++++ modules/python/init.zsh | 41 +++++++++++++++++++++++----------------- runcoms/zpreztorc | 3 +++ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/modules/python/README.md b/modules/python/README.md index 992af3b..d4cd571 100644 --- a/modules/python/README.md +++ b/modules/python/README.md @@ -56,6 +56,20 @@ is used. Replace *Developer* with your projects directory. export PROJECT_HOME="$HOME/Developer" ``` +The variable `VIRTUALENVWRAPPER_PYTHON` tells virtualenvwrapper to use the +specified full path of `python` interpreter overriding the `$PATH` search. + +```sh +export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python +``` + +The variable `VIRTUALENVWRAPPER_VIRTUALENV` tells virtualenvwrapper to use the +specified full path of `virtualenv` binary overriding the `$PATH` search. + +```sh +export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv +``` + The variable `$VIRTUALENVWRAPPER_VIRTUALENV_ARGS` tells virtualenvwrapper what arguments to pass to `virtualenv`. For example, set the value to *--system-site-packages* to ensure that all new environments have access to the @@ -76,10 +90,20 @@ This can be enabled with: zstyle ':prezto:module:python:virtualenv' auto-switch 'yes' ``` +virtualenvwrapper is automatically initialized if pre-requisites are met +(`$VIRTUALENVWRAPPER_VIRTUALENV` is explicitly set or `virtualenv` is in +`$PATH`). This can be disabled with: + +``` +zstyle ':prezto:module:python:virtualenv' initialize 'no' +``` + Aliases ------- - `py` is short for `python`. + - `py2` is short for `python2`. + - `py3` is short for `python3`. Functions --------- diff --git a/modules/python/init.zsh b/modules/python/init.zsh index d95b28a..759b903 100644 --- a/modules/python/init.zsh +++ b/modules/python/init.zsh @@ -82,26 +82,32 @@ if zstyle -t ':prezto:module:python:virtualenv' auto-switch 'yes'; then add-zsh-hook chpwd _python-workon-cwd fi -# Load virtualenvwrapper into the shell session, unless requested not to -if zstyle -T ':prezto:module:python' skip-virtualenvwrapper-init; then +# Load virtualenvwrapper into the shell session, if pre-requisites are met +# and unless explicitly requested not to +if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) && \ + zstyle -T ':prezto:module:python:virtualenv' initialize ; then # Set the directory where virtual environments are stored. export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}" # Disable the virtualenv prompt. VIRTUAL_ENV_DISABLE_PROMPT=1 - if (( $+commands[pyenv-virtualenvwrapper] )); then - pyenv virtualenvwrapper - elif (( $+commands[pyenv-virtualenv-init] )); then - eval "$(pyenv virtualenv-init -)" - elif (( $+commands[virtualenvwrapper_lazy.sh] )); then - source "$commands[virtualenvwrapper_lazy.sh]" - elif (( $+commands[virtualenvwrapper.sh] )); then - source "$commands[virtualenvwrapper.sh]" - elif [[ -f /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh ]]; then - source /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh - elif [[ -f /usr/share/virtualenvwrapper/virtualenvwrapper.sh ]]; then - source /usr/share/virtualenvwrapper/virtualenvwrapper.sh + if (( $+commands[pyenv] )); then + if (( $+commands[pyenv-virtualenv-init] )); then + eval "$(pyenv virtualenv-init -)" + fi + if (( $#commands[(i)pyenv-virtualenvwrapper(_lazy|)] )); then + pyenv "${${(@O)commands[(I)pyenv-virtualenvwrapper(_lazy|)]}[1]#pyenv-}" + fi + else + # Try 'virtualenvwrapper' without 'pyenv' wrapper in '$path' and other + # known locations on a Debian based system. + virtenv_sources=( + ${(@Ov)commands[(I)virtualenvwrapper(_lazy|).sh]} + /usr/share/virtualenvwrapper/virtualenvwrapper(_lazy|).sh(OnN) + ) + source "${virtenv_sources[1]}" + unset virtenv_sources fi fi @@ -109,7 +115,7 @@ fi if (( $#commands[(i)pip(|[23])] )); then cache_file="${0:h}/cache.zsh" - # Detect and use first one available among 'pip', 'pip2', 'pip3' variants + # Detect and use one available from among 'pip', 'pip2', 'pip3' variants pip_command="$commands[(i)pip(|[23])]" if [[ "$pip_command" -nt "$cache_file" || ! -s "$cache_file" ]]; then @@ -119,8 +125,7 @@ if (( $#commands[(i)pip(|[23])] )); then fi source "$cache_file" - unset cache_file - unset pip_command + unset cache_file pip_command fi # @@ -128,3 +133,5 @@ fi # alias py='python' +alias py2='python2' +alias py3='python3' diff --git a/runcoms/zpreztorc b/runcoms/zpreztorc index 22b5b3d..9a768df 100644 --- a/runcoms/zpreztorc +++ b/runcoms/zpreztorc @@ -123,6 +123,9 @@ zstyle ':prezto:module:prompt' theme 'sorin' # Auto switch the Python virtualenv on directory change. # zstyle ':prezto:module:python:virtualenv' auto-switch 'yes' +# Automatically initialize virtualenvwrapper if pre-requisites are met. +# zstyle ':prezto:module:python:virtualenv' initialize 'yes' + # # Screen #