Created
November 12, 2023 09:15
-
-
Save Zejnilovic/976301532f3af1612fae2b737aa08103 to your computer and use it in GitHub Desktop.
Function to easily select venv if you have more then one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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