Snippet to automatically activate an existing virtual environment or initialize and activate a new one.
Usage: add to your .zshrc, source it, then just run pyvenv.
pyvenv() {
# Check if .venv directory already exists
if [ -d ".venv" ]; then
echo "Found existing environment. Activating..."
else
echo "Creating new virtual environment..."
python3 -m venv .venv
# Automatically add .venv to .gitignore if it's not already there
if [ -f ".gitignore" ]; then
if ! grep -qs "^.venv/" .gitignore; then
echo ".venv/" >> .gitignore
echo "Added .venv/ to .gitignore."
fi
fi
fi
# Activate and confirm
source .venv/bin/activate && \
echo "Virtual environment at $(pwd)/.venv activated!"
}
