diff --git a/plugins/python/README.md b/plugins/python/README.md index eeaa1148a..4968cca6d 100644 --- a/plugins/python/README.md +++ b/plugins/python/README.md @@ -10,6 +10,7 @@ plugins=(... python) ## Aliases +<<<<<<< HEAD | Command | Description | | ---------------- | ------------------------------------------------------------------------------------- | | `py` | Runs `python` | @@ -19,3 +20,11 @@ plugins=(... python) | `pygrep ` | Looks for `text` in .py files | | `pyuserpaths` | Add --user site-packages to PYTHONPATH, for all installed python versions. | | `pyserver` | Starts an http.server on the current directory. Use `--directory` for a different one | + +## Virtual environments + +The plugin provides two utilities to manage Python venvs: + +- `mkv [name]`: make a new virtual environment called `name` (default: `venv`) in current directory. + +- `vrun [name]`: activate virtual environment called `name` (default: `venv`) in current directory. diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh index 896fae3ca..6c7fc6cbb 100644 --- a/plugins/python/python.plugin.zsh +++ b/plugins/python/python.plugin.zsh @@ -51,3 +51,36 @@ alias ipython="python -c 'import IPython; IPython.terminal.ipapp.launch_new_inst # Share local directory as a HTTP server alias pyserver="python -m http.server" + + +## venv utilities + +# Activate a the python virtual environment specified. +# If none specified, use 'venv'. +function vrun() { + local name="${1:-venv}" + local venvpath="${name:P}" + + if [[ ! -d "$venvpath" ]]; then + echo >&2 "Error: no such venv in current directory: $name" + return 1 + fi + + if [[ ! -f "${venvpath}/bin/activate" ]]; then + echo >&2 "Error: '${name}' is not a proper virtual environment" + return 1 + fi + + . "${venvpath}/bin/activate" || return $? + echo "Activated virtual environment ${name}" +} + +# Create a new virtual environment, with default name 'venv'. +function mkv() { + local name="${1:-venv}" + local venvpath="${name:P}" + + python3 -m venv "${name}" || return + echo >&2 "Created venv in '${venvpath}'" + vrun "${name}" +}