Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): add auto venv activation #12248

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions plugins/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ plugins=(... python)

## Virtual environments

The plugin provides two utilities to manage Python venvs:
The plugin provides three utilities to manage Python 3.3+ [venv](https://docs.python.org/3/library/venv.html)
virtual environments:

- `mkv [name]`: make a new virtual environment called `name` (default: `venv`) in current directory.
- `mkv [name]`: make a new virtual environment called `name` (default: if set `$PYTHON_VENV_NAME`, else
`venv`) in the current directory.

- `vrun [name]`: activate virtual environment called `name` (default: `venv`) in current directory.
- `vrun [name]`: Activate the virtual environment called `name` (default: if set `$PYTHON_VENV_NAME`, else
`venv`) in the current directory.

- `auto_vrun`: Automatically activate the venv virtual environment when entering a directory containing
`<venv-name>/bin/activate`, and automatically deactivate it when navigating out of it (including
subdirectories!).
- To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh.
- The default virtual environment name is `venv`. To use a different name, set
`export PYTHON_VENV_NAME=<venv-name>`. For example: `export PYTHON_VENV_NAME=".venv"`
23 changes: 19 additions & 4 deletions plugins/python/python.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ alias pyserver="python3 -m http.server"


## venv utilities
: ${PYTHON_VENV_NAME:=venv}

# Activate a the python virtual environment specified.
# If none specified, use 'venv'.
# If none specified, use $PYTHON_VENV_NAME, else 'venv'.
function vrun() {
local name="${1:-venv}"
local name="${1:-$PYTHON_VENV_NAME}"
local venvpath="${name:P}"

if [[ ! -d "$venvpath" ]]; then
Expand All @@ -72,12 +73,26 @@ function vrun() {
echo "Activated virtual environment ${name}"
}

# Create a new virtual environment, with default name 'venv'.
# Create a new virtual environment using the specified name.
# If none specfied, use $PYTHON_VENV_NAME
function mkv() {
local name="${1:-venv}"
local name="${1:-$PYTHON_VENV_NAME}"
local venvpath="${name:P}"

python3 -m venv "${name}" || return
echo >&2 "Created venv in '${venvpath}'"
vrun "${name}"
}

if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
# Automatically activate venv when changing dir
auto_vrun() {
if [[ -f "${PYTHON_VENV_NAME}/bin/activate" ]]; then
source "${PYTHON_VENV_NAME}/bin/activate" > /dev/null 2>&1
else
(( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
fi
}
add-zsh-hook chpwd auto_vrun
auto_vrun
fi