Skip to content

Instantly share code, notes, and snippets.

@robbles
Forked from codysoyland/virtualenv-auto-activate.sh
Last active November 7, 2023 14:01
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save robbles/2211471 to your computer and use it in GitHub Desktop.
Save robbles/2211471 to your computer and use it in GitHub Desktop.
virtualenv-auto-activate with support for zsh and virtualenvwrapper
#!/bin/bash
# virtualenv-auto-activate.sh
#
# Installation:
# Add this line to your .bashrc or .bash-profile:
#
# source /path/to/virtualenv-auto-activate.sh
#
# Go to your project folder, run "virtualenv .venv", so your project folder
# has a .venv folder at the top level, next to your version control directory.
# For example:
# .
# ├── .git
# │ ├── HEAD
# │ ├── config
# │ ├── description
# │ ├── hooks
# │ ├── info
# │ ├── objects
# │ └── refs
# └── .venv
# ├── bin
# ├── include
# └── lib
#
# The virtualenv will be activated automatically when you enter the directory.
# Check for virtualenvwrapper
if type workon >/dev/null 2>&1; then
VENV_WRAPPER=true
else
VENV_WRAPPER=false
fi
function _virtualenv_auto_activate() {
if [ -e ".venv" ]; then
# Check for symlink pointing to virtualenv
if [ -L ".venv" ]; then
_VENV_PATH=$(readlink .venv)
_VENV_WRAPPER_ACTIVATE=false
# Check for directory containing virtualenv
elif [ -d ".venv" ]; then
_VENV_PATH=$(pwd -P)/.venv
_VENV_WRAPPER_ACTIVATE=false
# Check for file containing name of virtualenv
elif [ -f ".venv" -a $VENV_WRAPPER = "true" ]; then
_VENV_PATH=$WORKON_HOME/$(cat .venv)
_VENV_WRAPPER_ACTIVATE=true
else
return
fi
# Check to see if already activated to avoid redundant activating
if [ "$VIRTUAL_ENV" != $_VENV_PATH ]; then
if $_VENV_WRAPPER_ACTIVATE; then
_VENV_NAME=$(basename $_VENV_PATH)
workon $_VENV_NAME
else
_VENV_NAME=$(basename `pwd`)
VIRTUAL_ENV_DISABLE_PROMPT=1
source .venv/bin/activate
_OLD_VIRTUAL_PS1="$PS1"
PS1="($_VENV_NAME)$PS1"
export PS1
fi
echo Activated virtualenv \"$_VENV_NAME\".
fi
fi
}
export PROMPT_COMMAND=_virtualenv_auto_activate
if [ -n "$ZSH_VERSION" ]; then
function chpwd() {
_virtualenv_auto_activate
}
fi
@robbles
Copy link
Author

robbles commented Jun 3, 2012

EDIT: changed to only use virtualenvwrapper for files containing name of venv, not for directories/symlinks

@coreybrett
Copy link

Any idea how I could make this work when I source it from my ~/.bash_aliases file? The script runs and exports PROMPT_COMMAND, but the _virtualenv_auto_activate function doesn't get defined for some reason.

@RobertDeRose
Copy link

@coreybrett I've forked a fork that's based off this that works on mac/linux using either bash or zsh, but more importantly, it fits the oh-my-zsh plugin architecture so you can add it as a plugin if you give oh-my-zsh and zsh a try.

@metersk
Copy link

metersk commented Apr 25, 2018

what would one need to change in this script to make the venv automatically activate after running virtualenv .venv? Right now, I need to cd out of and back into the project directory to activate it.

@RobertDeRose
Copy link

RobertDeRose commented May 13, 2022

what would one need to change in this script to make the venv automatically activate after running virtualenv .venv? Right now, I need to cd out of and back into the project directory to activate it.

If you use my fork, https://github.com/RobertDeRose/virtualenv-autodetect.git you just have to do cd .

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