Skip to content

Instantly share code, notes, and snippets.

@SiLiKhon
Last active February 19, 2026 05:35
Show Gist options
  • Select an option

  • Save SiLiKhon/207cb9fb159cf19f8f4f55e90b3b6f7c to your computer and use it in GitHub Desktop.

Select an option

Save SiLiKhon/207cb9fb159cf19f8f4f55e90b3b6f7c to your computer and use it in GitHub Desktop.
Simple bash function to detect venvs below the current directory.
#!/bin/bash
venvs() {
local DEPTH=3
local OPTIND=1
while getopts "l:" opt; do
case $opt in
l) DEPTH="$OPTARG" ;;
esac
done
echo "Looking for venvs in current dir (recursion depth is $DEPTH; use -l argument to change)"
VENVS=$(find . -maxdepth $DEPTH -type f -name pyvenv.cfg)
local VENVS_WITH_ACTIVATE_SCRIPTS=()
local ACTIVATE_SUFFIX="/bin/activate"
for VENV in $VENVS; do
local VENV_BASE=$(dirname "$VENV")
local ACTIVATE_SCRIPT=$VENV_BASE$ACTIVATE_SUFFIX
if [ -f "$ACTIVATE_SCRIPT" ]; then
VENVS_WITH_ACTIVATE_SCRIPTS+=("$VENV_BASE")
fi
done
if [ -z "$VENVS_WITH_ACTIVATE_SCRIPTS" ]; then
echo "No venvs found"
return 1
fi
echo "The virtual envs listed below were found. Pick the one to activate (^C to cancel):"
select SCRIPT in "${VENVS_WITH_ACTIVATE_SCRIPTS[@]}"; do
if [[ -n $SCRIPT ]]; then
source "$SCRIPT$ACTIVATE_SUFFIX"
break
else
echo "Invalid choice. Please try again."
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment