Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deactivate Conda environment before virtualenv activation #177

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ By default `mkvenv` will install setup.py via pip in `editable (i.e. development
<https://pip.pypa.io/en/stable/cli/pip_install/#editable-installs>`__.
To change this set ``AUTOSWITCH_PIPINSTALL`` to ``FULL``.

**Deactivating active Conda environment**

By default, virtual environments are activated without checking if any Conda environment is active.
When one is, your prompt may display information about virtualenv being active, but in reality
the Conda environment will still be used.

To avoid such confusing behaviour, you can have the Conda environment deactivated automatically
(and then switched back when leaving the directory), set the value of the environment variable
``AUTOSWITCH_DEACTIVATE_CONDA`` to a non-empty value.

Security Warnings
-----------------

Expand Down
12 changes: 12 additions & 0 deletions autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ function _maybeworkon() {
return
fi

if [[ ! -z "$AUTOSWITCH_DEACTIVATE_CONDA" ]] && [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
_autoswitch_message "Deactivating Conda env: ${BOLD}${GREEN}%s${NORMAL}\n" "$CONDA_DEFAULT_ENV"
export AUTOSWITCH_DEACTIVATED_CONDA_ENV=$CONDA_DEFAULT_ENV
conda deactivate
fi

local py_version="$(_python_version "$venv_dir/bin/python")"
local message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
message="${message//\%venv_type/$venv_type}"
Expand Down Expand Up @@ -231,6 +237,12 @@ function _default_venv()
local venv_name="$(_get_venv_name "$VIRTUAL_ENV" "$venv_type")"
_autoswitch_message "Deactivating: ${BOLD}${PURPLE}%s${NORMAL}\n" "$venv_name"
deactivate

if [[ ! -z "$AUTOSWITCH_DEACTIVATE_CONDA" ]] && [[ ! -z "$AUTOSWITCH_DEACTIVATED_CONDA_ENV" ]]; then
_autoswitch_message "Switching Conda env: ${BOLD}${GREEN}%s${NORMAL}\n" "$AUTOSWITCH_DEACTIVATED_CONDA_ENV"
conda activate $AUTOSWITCH_DEACTIVATED_CONDA_ENV
unset AUTOSWITCH_DEACTIVATED_CONDA_ENV
fi
fi
}

Expand Down