Skip to content

Instantly share code, notes, and snippets.

@robin-collins
Created September 2, 2023 00:58
Show Gist options
  • Save robin-collins/5f9c66e4bad285284575220c422a1344 to your computer and use it in GitHub Desktop.
Save robin-collins/5f9c66e4bad285284575220c422a1344 to your computer and use it in GitHub Desktop.
function activatevenv() {
# Check if a virtual environment is already activated
if [ -z "$VIRTUAL_ENV" ]; then
# Names of possible virtualenv directories
VIRTUALENV_DIRS=("venv/" "env/" ".env/" ".venv/" "${PWD##*/}")
for dir in "${VIRTUALENV_DIRS[@]}"; do
if [[ -d "${dir}" ]]; then
# Found a possible venv directory
# Try activating the venv
if [[ -e "./${dir}/bin/activate" ]]; then
source ./$dir/bin/activate
echo "Virtual environment activated automatically"
# Store the absolute path of the activated virtual environment directory
export VENV_PATH=$(realpath ./)
break
fi
fi
done
fi
}
activatevenv
# Extension for `cd` command in order to automatically activate virtualenv when changin directories.
function cd() {
old_dir=$(realpath .) # Use realpath to get the absolute path
target_dir=$1
builtin cd "${target_dir:-$HOME}"
# If we go up from the VENV_PATH or go somewhere not inside VENV_PATH
if [[ "${old_dir}" == "${VENV_PATH}"* ]] && [[ "$(realpath .)" != "${VENV_PATH}"* ]]; then
echo "Leaving python virtual environment"
deactivate
unset VENV_PATH
fi
# Try activating venv
activatevenv
}

This is a custom shell script I modified in order to ease my workflow when dealing with Python Virtual Environments.

Original version found @ https://gist.github.com/kishannareshpal/342efc4a15e47ea5d338784d3e9a8d98 Original version only activated Pthon Virtual Environment on entering the directory. This updated versions loads venv upon entering directory, and unloads it when you traverse the directory tree UP out of the directory. It does not unload the venv if you traverse DOWN the directory tree. I do not use zsh, I use BASH so I cannot deny or confirm if the zsh instructions work, they were copied from original source for completeness.

Installation

Download the activatevenv.plugin.sh into your computer.

oh-my-zsh

  1. Download the script into $ZSH_CUSTOM/plugins/<plugin_name>/activatevenv/. See oh-my-zsh Plugin Customization Wiki
  # Use curl or download manually from https://git.io/JLBHr
  (mkdir activatevenv; cd activatevenv) && curl -L --url https://git.io/JLBHr --output ./activatevenv/activatevenv.plugin.zsh
  1. Edit your ~/.zshrc:
# Add activatevenv to your list of plugins and restart the terminal.
  plugins=(... , activatevenv)
  …

Manually

  1. Copy and paste the activatevend.plugin.sh script to the bottom of your ~/.bashrc or ~/.zshrc and restart your terminal.

Usage

Simply cd into a project directory with a python virtual environment setup (with any of these names: venv/, .venv/, env or .env), and the script will activate it automatically for you (just as you would do with source ./venv/bin/activate).

If you are creating a new virtualenv, run python -m <venv|virtualenv> <venv|.venv|.env|env> in your root directory and to activate it manually call activatevenv (also when you cd back into your project folder it will automatically activate it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment