_conda_pack_deactivate () {
    # If there's an active environment
    if [ -n "$CONDA_PREFIX" ]; then
        # First run the deactivate scripts
        local _path
        for _path in "$CONDA_PREFIX/etc/conda/deactivate.d/*"; do
            if [ -f "$_path" ]; then
                . "$_path"
            fi
        done

        # Remove env/bin from path
        local IFS=':'
        local _target="$CONDA_PREFIX/bin"
        local _newpath
        local _path

        for _path in $PATH; do
            if [ "$_path" != "$_target" ] ; then
                _newpath=${_newpath:+$_newpath:}$_path
            fi
        done

        PATH="$_newpath"
        unset CONDA_PREFIX
        PS1="$_CONDA_PACK_OLD_PS1"
        unset _CONDA_PACK_OLD_PS1
    fi
}

_conda_pack_deactivate
