Skip to content

Instantly share code, notes, and snippets.

@Zejnilovic
Created November 12, 2023 09:15
Show Gist options
  • Save Zejnilovic/976301532f3af1612fae2b737aa08103 to your computer and use it in GitHub Desktop.
Save Zejnilovic/976301532f3af1612fae2b737aa08103 to your computer and use it in GitHub Desktop.
Function to easily select venv if you have more then one.
function activate_env() {
# Get
local filtered_envs=($(find . -maxdepth 1 -type d -exec test -e '{}/bin/activate' \; -print))
# If no environments found
if [[ ${#filtered_envs[@]} -eq 0 ]]; then
echo "No suitable environments found."
return
fi
# If only one environment is found
if [[ ${#filtered_envs[@]} -eq 1 ]]; then
echo "Activating environment: ${filtered_envs[1]}"
source "${filtered_envs[1]}/bin/activate"
return
fi
# If multiple environments are found
echo "Multiple environments found. Please select one:"
local index=1
for env in "${filtered_envs[@]}"; do
echo "$index) $env"
index=$((index+1))
done
read choice\?"Enter number (1-$(($index-1))): "
if [[ choice -ge 0 && choice -lt $index ]]; then
echo "Activating environment: ${filtered_envs[choice]}"
source "${filtered_envs[choice]}/bin/activate"
else
echo "Invalid selection."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment