Browse Source

[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
master
Indrajit Raychaudhuri 7 years ago committed by Indrajit Raychaudhuri
parent
commit
19435b16ea
  1. 24
      modules/python/README.md
  2. 41
      modules/python/init.zsh
  3. 3
      runcoms/zpreztorc

24
modules/python/README.md

@ -56,6 +56,20 @@ is used. Replace *Developer* with your projects directory.
export PROJECT_HOME="$HOME/Developer" 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 The variable `$VIRTUALENVWRAPPER_VIRTUALENV_ARGS` tells virtualenvwrapper what
arguments to pass to `virtualenv`. For example, set the value to arguments to pass to `virtualenv`. For example, set the value to
*--system-site-packages* to ensure that all new environments have access to the *--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' 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 Aliases
------- -------
- `py` is short for `python`. - `py` is short for `python`.
- `py2` is short for `python2`.
- `py3` is short for `python3`.
Functions Functions
--------- ---------

41
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 add-zsh-hook chpwd _python-workon-cwd
fi fi
# Load virtualenvwrapper into the shell session, unless requested not to # Load virtualenvwrapper into the shell session, if pre-requisites are met
if zstyle -T ':prezto:module:python' skip-virtualenvwrapper-init; then # 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. # Set the directory where virtual environments are stored.
export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}" export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}"
# Disable the virtualenv prompt. # Disable the virtualenv prompt.
VIRTUAL_ENV_DISABLE_PROMPT=1 VIRTUAL_ENV_DISABLE_PROMPT=1
if (( $+commands[pyenv-virtualenvwrapper] )); then if (( $+commands[pyenv] )); then
pyenv virtualenvwrapper if (( $+commands[pyenv-virtualenv-init] )); then
elif (( $+commands[pyenv-virtualenv-init] )); then eval "$(pyenv virtualenv-init -)"
eval "$(pyenv virtualenv-init -)" fi
elif (( $+commands[virtualenvwrapper_lazy.sh] )); then if (( $#commands[(i)pyenv-virtualenvwrapper(_lazy|)] )); then
source "$commands[virtualenvwrapper_lazy.sh]" pyenv "${${(@O)commands[(I)pyenv-virtualenvwrapper(_lazy|)]}[1]#pyenv-}"
elif (( $+commands[virtualenvwrapper.sh] )); then fi
source "$commands[virtualenvwrapper.sh]" else
elif [[ -f /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh ]]; then # Try 'virtualenvwrapper' without 'pyenv' wrapper in '$path' and other
source /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh # known locations on a Debian based system.
elif [[ -f /usr/share/virtualenvwrapper/virtualenvwrapper.sh ]]; then virtenv_sources=(
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh ${(@Ov)commands[(I)virtualenvwrapper(_lazy|).sh]}
/usr/share/virtualenvwrapper/virtualenvwrapper(_lazy|).sh(OnN)
)
source "${virtenv_sources[1]}"
unset virtenv_sources
fi fi
fi fi
@ -109,7 +115,7 @@ fi
if (( $#commands[(i)pip(|[23])] )); then if (( $#commands[(i)pip(|[23])] )); then
cache_file="${0:h}/cache.zsh" 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])]" pip_command="$commands[(i)pip(|[23])]"
if [[ "$pip_command" -nt "$cache_file" || ! -s "$cache_file" ]]; then if [[ "$pip_command" -nt "$cache_file" || ! -s "$cache_file" ]]; then
@ -119,8 +125,7 @@ if (( $#commands[(i)pip(|[23])] )); then
fi fi
source "$cache_file" source "$cache_file"
unset cache_file unset cache_file pip_command
unset pip_command
fi fi
# #
@ -128,3 +133,5 @@ fi
# #
alias py='python' alias py='python'
alias py2='python2'
alias py3='python3'

3
runcoms/zpreztorc

@ -123,6 +123,9 @@ zstyle ':prezto:module:prompt' theme 'sorin'
# Auto switch the Python virtualenv on directory change. # Auto switch the Python virtualenv on directory change.
# zstyle ':prezto:module:python:virtualenv' auto-switch 'yes' # zstyle ':prezto:module:python:virtualenv' auto-switch 'yes'
# Automatically initialize virtualenvwrapper if pre-requisites are met.
# zstyle ':prezto:module:python:virtualenv' initialize 'yes'
# #
# Screen # Screen
# #

Loading…
Cancel
Save