mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2025-12-07 07:50:40 +01:00
Updated Python plugin: accepts multiple venv names
- Refined automatic virtual environment activation (`auto_vrun`):
- Enhanced support for dynamic activation of virtual environments from a list.
- Updated `vrun` function:
- Enabled activation of the first valid virtual environment from a list of potential names.
- Improved feedback with clear error messages when no valid environment is found.
- Enhanced `mkv` function:
- Added support for multiple potential virtual environment names using `PYTHON_VENV_NAME` as a list.
- Simplified path handling with explicit construction using `${PWD}`.
- Improved error messaging and direct activation of newly created environments.
- This is my first pull request ever so please let me know if there are any issues with the changes or request.
This commit is contained in:
@@ -36,4 +36,5 @@ virtual environments:
|
|||||||
- To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh.
|
- To enable the feature, set `export PYTHON_AUTO_VRUN=true` before sourcing oh-my-zsh.
|
||||||
- Plugin activates first virtual environment in lexicographic order whose name begins with `<venv-name>`.
|
- Plugin activates first virtual environment in lexicographic order whose name begins with `<venv-name>`.
|
||||||
The default virtual environment name is `venv`. To use a different name, set
|
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"`
|
`export PYTHON_VENV_NAME=("<venv-name>" "venv-name2")`. For example:
|
||||||
|
`export PYTHON_VENV_NAME=(".venv" "venv" ".env" "env")`
|
||||||
|
|||||||
@@ -46,35 +46,32 @@ alias pygrep='grep -nr --include="*.py"'
|
|||||||
# Share local directory as a HTTP server
|
# Share local directory as a HTTP server
|
||||||
alias pyserver="python3 -m http.server"
|
alias pyserver="python3 -m http.server"
|
||||||
|
|
||||||
|
|
||||||
## venv utilities
|
|
||||||
: ${PYTHON_VENV_NAME:=venv}
|
: ${PYTHON_VENV_NAME:=venv}
|
||||||
|
|
||||||
# Activate a the python virtual environment specified.
|
# Activate the first valid Python virtual environment from a list.
|
||||||
# If none specified, use $PYTHON_VENV_NAME, else 'venv'.
|
|
||||||
function vrun() {
|
function vrun() {
|
||||||
local name="${1:-$PYTHON_VENV_NAME}"
|
local venv_list=("${PYTHON_VENV_NAME}")
|
||||||
local venvpath="${name:P}"
|
local venvpath
|
||||||
|
|
||||||
if [[ ! -d "$venvpath" ]]; then
|
# Loop through potential venv names
|
||||||
echo >&2 "Error: no such venv in current directory: $name"
|
for name in "${venv_list[@]}"; do
|
||||||
return 1
|
venvpath="${PWD}/${name}"
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ ! -f "${venvpath}/bin/activate" ]]; then
|
if [[ -d "$venvpath" && -f "${venvpath}/bin/activate" ]]; then
|
||||||
echo >&2 "Error: '${name}' is not a proper virtual environment"
|
. "${venvpath}/bin/activate" || return $?
|
||||||
return 1
|
echo "Activated virtual environment ${name}"
|
||||||
fi
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
. "${venvpath}/bin/activate" || return $?
|
echo >&2 "Error: No valid virtual environment found in the list: ${PYTHON_VENV_NAME[*]}"
|
||||||
echo "Activated virtual environment ${name}"
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create a new virtual environment using the specified name.
|
# Create a new virtual environment with the specified name or the first in the list.
|
||||||
# If none specified, use $PYTHON_VENV_NAME
|
|
||||||
function mkv() {
|
function mkv() {
|
||||||
local name="${1:-$PYTHON_VENV_NAME}"
|
local name="${1:-${PYTHON_VENV_NAME%% *}}"
|
||||||
local venvpath="${name:P}"
|
local venvpath="${PWD}/${name}"
|
||||||
|
|
||||||
python3 -m venv "${name}" || return
|
python3 -m venv "${name}" || return
|
||||||
echo >&2 "Created venv in '${venvpath}'"
|
echo >&2 "Created venv in '${venvpath}'"
|
||||||
@@ -82,22 +79,27 @@ function mkv() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
|
if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
|
||||||
# Automatically activate venv when changing dir
|
# Automatically activate the first valid venv and deactivate if leaving its root directory
|
||||||
function auto_vrun() {
|
function auto_vrun() {
|
||||||
# deactivate if we're on a different dir than VIRTUAL_ENV states
|
# If we're inside a virtual environment, check if we're leaving its root
|
||||||
# we don't deactivate subdirectories!
|
if [[ -n "${VIRTUAL_ENV}" ]]; then
|
||||||
if (( $+functions[deactivate] )) && [[ $PWD != ${VIRTUAL_ENV:h}* ]]; then
|
local venv_root="${VIRTUAL_ENV%/bin*}" # Extract the root directory of the virtual environment
|
||||||
deactivate > /dev/null 2>&1
|
# Deactivate if we are outside the root directory of the virtual environment
|
||||||
|
if [[ "$PWD" != "$venv_root"* ]]; then
|
||||||
|
echo "Deactivating virtual environment"
|
||||||
|
deactivate > /dev/null 2>&1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $PWD != ${VIRTUAL_ENV:h} ]]; then
|
# Attempt to activate a venv from the list in the current directory
|
||||||
for _file in "${PYTHON_VENV_NAME}"*/bin/activate(N.); do
|
for _venv_name in "${PYTHON_VENV_NAME[@]}"; do
|
||||||
# make sure we're not in a venv already
|
local activate_script="${PWD}/${_venv_name}/bin/activate"
|
||||||
(( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
|
if [[ -f "${activate_script}" ]]; then
|
||||||
source $_file > /dev/null 2>&1
|
source "${activate_script}" > /dev/null 2>&1
|
||||||
break
|
echo "Automatically activated virtual environment ${_venv_name}"
|
||||||
done
|
return 0
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
add-zsh-hook chpwd auto_vrun
|
add-zsh-hook chpwd auto_vrun
|
||||||
auto_vrun
|
auto_vrun
|
||||||
|
|||||||
Reference in New Issue
Block a user