Diff
1diff --git a/plugins/python/README.md b/plugins/python/README.md
2index 7bf1b34acc3ab174109b19b69a597d579ef771ee..c99697b22c1e1e4c2bd74df489722e0355619e31 100644
3--- a/plugins/python/README.md
4+++ b/plugins/python/README.md
5@@ -22,8 +22,18 @@ plugins=(... python)
6
7 ## Virtual environments
8
9-The plugin provides two utilities to manage Python venvs:
10+The plugin provides three utilities to manage Python 3.3+ [venv](https://docs.python.org/3/library/venv.html)
11+virtual environments:
12
13-- `mkv [name]`: make a new virtual environment called `name` (default: `venv`) in current directory.
14+- `mkv [name]`: make a new virtual environment called `name` (default: if set `$PYTHON_VENV_NAME`, else
15+ `venv`) in the current directory.
16
17-- `vrun [name]`: activate virtual environment called `name` (default: `venv`) in current directory.
18+- `vrun [name]`: Activate the virtual environment called `name` (default: if set `$PYTHON_VENV_NAME`, else
19+ `venv`) in the current directory.
20+
21+- `auto_vrun`: Automatically activate the venv virtual environment when entering a directory containing
22+ `<venv-name>/bin/activate`, and automatically deactivate it when navigating out of it (including
23+ subdirectories!).
24+ - To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh.
25+ - The default virtual environment name is `venv`. To use a different name, set
26+ `export PYTHON_VENV_NAME=<venv-name>`. For example: `export PYTHON_VENV_NAME=".venv"`
27diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh
28index 77d4bf42500a662b5a4059f44c2e8a2ff580a7d7..f6ea85027cb4d8fe0969d15a718059a4910a896e 100644
29--- a/plugins/python/python.plugin.zsh
30+++ b/plugins/python/python.plugin.zsh
31@@ -51,11 +51,12 @@ alias pyserver="python3 -m http.server"
32
33
34 ## venv utilities
35+: ${PYTHON_VENV_NAME:=venv}
36
37 # Activate a the python virtual environment specified.
38-# If none specified, use 'venv'.
39+# If none specified, use $PYTHON_VENV_NAME, else 'venv'.
40 function vrun() {
41- local name="${1:-venv}"
42+ local name="${1:-$PYTHON_VENV_NAME}"
43 local venvpath="${name:P}"
44
45 if [[ ! -d "$venvpath" ]]; then
46@@ -72,12 +73,26 @@ function vrun() {
47 echo "Activated virtual environment ${name}"
48 }
49
50-# Create a new virtual environment, with default name 'venv'.
51+# Create a new virtual environment using the specified name.
52+# If none specfied, use $PYTHON_VENV_NAME
53 function mkv() {
54- local name="${1:-venv}"
55+ local name="${1:-$PYTHON_VENV_NAME}"
56 local venvpath="${name:P}"
57
58 python3 -m venv "${name}" || return
59 echo >&2 "Created venv in '${venvpath}'"
60 vrun "${name}"
61 }
62+
63+if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
64+ # Automatically activate venv when changing dir
65+ auto_vrun() {
66+ if [[ -f "${PYTHON_VENV_NAME}/bin/activate" ]]; then
67+ source "${PYTHON_VENV_NAME}/bin/activate" > /dev/null 2>&1
68+ else
69+ (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
70+ fi
71+ }
72+ add-zsh-hook chpwd auto_vrun
73+ auto_vrun
74+fi