prezto/modules/python
Jeff Widman e3a9583f33 Support virtualenvwrapper with / without pyenv virtualenv-init or virtualenvwrapper plugins
The desired logic is:

For the `pyenv` plugins `virtualenv-init` and `virtualenvwrapper`:
1. If either plugin is present, activate it
2. If `virtualenvwrapper` plugin is not present, then
   [fallback to standard
   `virtualenvwrapper`](https://github.com/sorin-ionescu/prezto/pull/1414#issuecomment-320306421).
3. If `virtualenvwrapper` plugin is present, then [don't fallback to
   standard `virtualenvwrapper`, regardless of whether `virtualenv-init`
   is
   present](https://github.com/sorin-ionescu/prezto/pull/1981#issue-1123766676).

Previously, if the `virtualenv` command was present but `pyenv` was
missing, then the fallback wouldn't be hit. This bug was introduced by
https://github.com/sorin-ionescu/prezto/pull/1981/ which ensured that
the `pyenv` `virtualenvwrapper` plugin was activated if present,
regardless of the presence of the `virtualenv-init` plugin.

As an optimization, the check for the `pyenv` plugins are skipped if
`pyenv` itself isn't found.

Since we only want to fallback if the `pyenv` `virtualenvwrapper` plugin
is missing, but that's buried within the `pyenv` logic and we also need
to handle when `pyenv` itself is missing, this switches to using a flag
variable.

I also renamed the `virtualenv_sources` var to
`virtualenvwrapper_sources` as `virtualenv` is distinct from
`virtualenvwrapper`, so using one name for a var that is really about
the other is confusing.

Looking at `git blame`, there's a _lot_ of prior art here around trying
to support all the permutations of `pyenv` and various plugins:
* https://github.com/sorin-ionescu/prezto/issues/1413
* https://github.com/sorin-ionescu/prezto/pull/1414
* https://github.com/sorin-ionescu/prezto/pull/1433
* https://github.com/sorin-ionescu/prezto/pull/1434

So we need to be extremely careful to continue to support all these
permutations.

Fix https://github.com/sorin-ionescu/prezto/issues/2022
2022-10-26 22:03:42 -07:00
..
functions python: Optimize completion for loading lazily on demand 2021-06-01 14:29:39 -05:00
init.zsh Support virtualenvwrapper with / without pyenv virtualenv-init or virtualenvwrapper plugins 2022-10-26 22:03:42 -07:00
README.md python: Update pyenv initialization flow per pyenv 2.0 2021-06-01 14:29:39 -05:00

Python

Enables local Python and local Python package installation.

This module must be loaded before the completion module so that the provided completion definitions are loaded.

Settings

This module supports virtual environments from conda and virtualenvwrapper. By default, only virtualenvwrapper is enabled. To disable virtualenvwrapper, add the following to ${ZDOTDIR:-$HOME}/.zpreztorc.

zstyle ':prezto:module:python' skip-virtualenvwrapper-init 'on'

Conda support can be enabled by adding the following to ${ZDOTDIR:-$HOME}/.zpreztorc.

zstyle ':prezto:module:python' conda-init 'on'

Caution: using conda and virtualenvwrapper at the same time may cause conflicts.

Local Python Installation

pyenv builds and installs multiple Python versions locally in the home directory.

This module prepends the pyenv directory to the path variable to enable the execution of pyenv.

Usage

Install Python versions with pyenv install into ~/.pyenv/versions.

This will be loaded automatically if pyenv is installed to $PYENV_ROOT, ~/.pyenv, or if the pyenv command is on the path. By default, PYENV_ROOT is set to $HOME/.pyenv. You can set it to an alternate location and export it in ${ZDOTDIR:-$HOME}/.zshenv.

Local Package Installation

Since version 2.6, Python supports per user package installation, as defined in PEP 370.

This module prepends per user site directories to the relevant path variables to enable the execution of user installed scripts and the reading of documentation.

Usage

Install packages into the per user site directory with pip install --user.

virtualenvwrapper

virtualenvwrapper is a frontend to the popular virtualenv utility.

virtualenv creates isolated Python environments and virtualenvwrapper provides convenient shell functions to create, switch, and manage them.

Usage

Install virtualenvwrapper.

Virtual environments are stored in ~/.virtualenvs.

There are configuration variables that have to be set to enable certain features. If you wish to use these features, export the variables in ${ZDOTDIR:-$HOME}/.zshenv.

The variable $PROJECT_HOME tells virtualenvwrapper where to place project working directories. It must be set and the directory created before mkproject 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 the python interpreter overriding the $PATH search.

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.

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 system site-packages directory.

export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--system-site-packages'

Additional Options

There is a hook to enable auto-switching to virtualenvs when switching into a directory where the root of the project matches a virtualenv name.

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

  • python-info exposes information about the Python environment via the $python_info associative array.

Theming

To display the name of the current virtual environment in a prompt, define the following style in the prompt_name_setup function.

# %v - virtualenv name.
zstyle ':prezto:module:python:info:virtualenv' format 'virtualenv:%v'

Then add $python_info[virtualenv] to $PROMPT or $RPROMPT and call python-info in the prompt_name_preexec hook function.

Similarly, you can use :prezto:module:python:info:version:format with %v for the version and add $python_info[version] to your prompt for the current python version.

Authors

The authors of this module should be contacted via the issue tracker.