@mods
I don’t see a coding or scripting sub forum here so just posting this here if any one wants to use it. Delete if necessary.
I just put it into my .bashrc and run by venv_activate, or make it a bash script and put into your $PATH
replaces source venv/bin/activate or source .venv/bin/activate
venv_activate() {
if [ -f .venv/bin/activate ]; then
echo "Activating virtual environment from: .venv in $PWD"
echo -e "Enjoy working on the $(basename "$PWD") project, ${USER^}!"
source .venv/bin/activate
# If not found, check for a venv directory
elif [ -f venv/bin/activate ]; then
echo "Activating virtual environment from: venv in $PWD"
echo -e "Enjoy working on the $(basename "$PWD") project, ${USER^}!"
source venv/bin/activate
else
echo "Error: No 'venv' or '.venv' directory found." >&2
return 1
fi
}
A neat shortcut. Adding the project name into the echo message is a nice touch—makes it clear which environment is currently active.
Made a better one. Little more color. I’m still kinda newish to scripts, but now I can’t stop writting them though. #geekfun
#!/bin/bash
Color_Off='\033[0m'
BGreen='\033[1;32m'
nl=$'\n'
venv_activate() {
enjoy() {
echo -e "Enjoy working on your $(basename "$PWD") project, ${USER^}!"
printf '*%.0s' {1..80}
}
spin_up() {
if [ -n "$1" ]; then #Check if a virtual environment path is provided.
bash -c "source $1/bin/activate; exec bash -i" || echo "Failed to activate: $1"
else
echo "Error: No virtual environment path provided to spin_up." >&2
return 1
fi
}
if [ -d "$PWD/.venv" ]; then ${nl}
printf '*%.0s' {1..80}
echo -e "${nl}$BGreen .venv$Color_Off found in: $PWD | $BGreen .venv$Color_Off activated.${nl}"
venv_path="$PWD/.venv"
enjoy
spin_up "$venv_path"
return 0
elif [ -d "$PWD/venv" ]; then
printf '*%.0s' {1..80}
echo -e "${nl}$BGreen venv$Color_Off found in: $PWD | $BGreen venv$Color_Off activated.${nl}"
venv_path="$PWD/venv"
enjoy
spin_up "$venv_path"
return 0
else
echo -e "Error: No 'venv' or '.venv' directory found." >&2
return 1
fi
}
venv_activate