2 # search in these locations for the init script:
3 for virtualenvwrapper in $commands[virtualenvwrapper_lazy.sh] \
4 $commands[virtualenvwrapper.sh] \
5 /usr/share/virtualenvwrapper/virtualenvwrapper{_lazy,}.sh \
6 /usr/local/bin/virtualenvwrapper{_lazy,}.sh \
7 /etc/bash_completion.d/virtualenvwrapper \
8 /usr/share/bash-completion/completions/virtualenvwrapper \
9 $HOME/.local/bin/virtualenvwrapper.sh
11 if [[ -f "$virtualenvwrapper" ]]; then
12 source "$virtualenvwrapper"
16 print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.\n"\
17 "Please install with \`pip install virtualenvwrapper\`" >&2
21 if [[ $? -eq 0 ]] && ! type workon &>/dev/null; then
22 print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
23 "Please check ${virtualenvwrapper}" >&2
27 if [[ -z "$WORKON_HOME" ]]; then
28 WORKON_HOME="$HOME/.virtualenvs"
31 if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
32 # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
33 # directory name of the project. Virtual environment name can be overridden
34 # by placing a .venv file in the project root with a virtualenv name in it.
36 if [[ -z "$WORKON_CWD" ]]; then
38 # Get absolute path, resolving symlinks
39 local PROJECT_ROOT="${PWD:A}"
40 while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
41 && ! -d "$PROJECT_ROOT/.git" ]]; do
42 PROJECT_ROOT="${PROJECT_ROOT:h}"
45 # Check for virtualenv name override
46 if [[ -f "$PROJECT_ROOT/.venv" ]]; then
47 ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
48 elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
49 ENV_NAME="$PROJECT_ROOT/.venv"
50 elif [[ "$PROJECT_ROOT" != "/" ]]; then
51 ENV_NAME="${PROJECT_ROOT:t}"
56 if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
57 # We've just left the repo, deactivate the environment
58 # Note: this only happens if the virtualenv was activated automatically
59 if [[ -n "$VIRTUAL_ENV" ]]; then
60 # Only deactivate if VIRTUAL_ENV was set
61 # User may have deactivated manually or via another mechanism
67 if [[ "$ENV_NAME" != "" ]]; then
68 # Activate the environment only if it is not already active
69 if [[ ! "$VIRTUAL_ENV" -ef "$WORKON_HOME/$ENV_NAME" ]]; then
70 if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
71 workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
72 elif [[ -e "$ENV_NAME/bin/activate" ]]; then
73 source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
79 if [[ "$ENV_NAME" == "" && -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
80 # We've just left the repo, deactivate the environment
81 # Note: this only happens if the virtualenv was activated automatically
82 deactivate && unset CD_VIRTUAL_ENV
87 # Append workon_cwd to the chpwd_functions array, so it will be called on cd
88 # http://zsh.sourceforge.net/Doc/Release/Functions.html
89 autoload -U add-zsh-hook
90 add-zsh-hook chpwd workon_cwd