Repository: conda/conda

Issue:
Source activation for anaconda python environments no longer working (Mac)
The source activate command in conda is suddenly no longer working for me. When I type `source activate py3` in my Mac terminal, I get this error `-bash: _conda_activate: command not found`.

I recently created a new conda environment for TabPy (Tableau python server); I'm not sure if that changed anything. I have checked that the conda environments I'm trying to activate exist by using `conda info --envs`, here is that output:

```
# conda environments:
#
base                  *  /Users/applemacbook/anaconda
Tableau-Python-Server     /Users/applemacbook/anaconda/envs/Tableau-Python 
Server
py3                      /Users/applemacbook/anaconda/envs/py3
```

I already have the following in my bash profile from my initial installation of anaconda awhile ago `export PATH="/Users/applemacbook/anaconda/bin:$PATH"`.

Here is some additional information: The output of `which conda` is `/Users/applemacbook/anaconda/bin/conda`. The output of `type source` is `source is a shell builtin`. The output of `which activate` is `/Users/applemacbook/anaconda/bin/activate`. My conda version is conda 4.4.6
Source activation for anaconda python environments no longer working (Mac)
The source activate command in conda is suddenly no longer working for me. When I type `source activate py3` in my Mac terminal, I get this error `-bash: _conda_activate: command not found`.

I recently created a new conda environment for TabPy (Tableau python server); I'm not sure if that changed anything. I have checked that the conda environments I'm trying to activate exist by using `conda info --envs`, here is that output:

```
# conda environments:
#
base                  *  /Users/applemacbook/anaconda
Tableau-Python-Server     /Users/applemacbook/anaconda/envs/Tableau-Python 
Server
py3                      /Users/applemacbook/anaconda/envs/py3
```

I already have the following in my bash profile from my initial installation of anaconda awhile ago `export PATH="/Users/applemacbook/anaconda/bin:$PATH"`.

Here is some additional information: The output of `which conda` is `/Users/applemacbook/anaconda/bin/conda`. The output of `type source` is `source is a shell builtin`. The output of `which activate` is `/Users/applemacbook/anaconda/bin/activate`. My conda version is conda 4.4.6


Discussion:
Guessing this is a duplicate of https://github.com/conda/conda/issues/6639?
Maybe not.  Could you open a new terminal, and then give me the output of

    env | sort
    set -x
    source activate py3
You'll want to close the terminal down after you do the above.  Or

    set +x

to get things back to normal.

Also, can you give me the output of 

    cat /Users/applemacbook/anaconda/bin/activate
Edited by moderator. Output given fixed-width below.



Alright, I think I've got all the outputs you asked for below:
```
$ env | sort
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.XLbN3qRfIV/Render
CDPATH=.:/Users/applemacbook:/Users/applemacbook/Documents/Galvanize:/Users/applemacbook/Documents/Galvanize/immersive:/Users/applemacbook/Documents:
COLORFGBG=7;0
COLORTERM=truecolor
EDITOR=emacs
HADOOP_HOME=/usr/local/hadoop
HOME=/Users/applemacbook
ITERM_PROFILE=Default
ITERM_SESSION_ID=w0t3p0:1061674E-3BF3-4203-A297-44BAC655F9A4
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
LANG=en_US.UTF-8
LOGNAME=applemacbook
PATH=/usr/local/spark/bin:/Users/applemacbook/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PS1=$
PWD=/Users/applemacbook
PYTHONPATH=/usr/local/spark/python:
SHELL=/bin/bash
SHLVL=1
SPARK_HOME=/usr/local/spark
SPARK_LOCAL_IP=localhost
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.FwpsT72RUz/Listeners
TERM=xterm-256color
TERM_PROGRAM=iTerm.app
TERM_PROGRAM_VERSION=3.1.5
TERM_SESSION_ID=w0t3p0:1061674E-3BF3-4203-A297-44BAC655F9A4
TMPDIR=/var/folders/yp/3f96txfd1rj8m8k28xxbvpkh0000gn/T/
USER=applemacbook
VISUAL=emacs
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
_=/usr/bin/env
__CF_USER_TEXT_ENCODING=0x0:0:0

$ set -x
+ set -x

$ source activate py3
+ source activate py3
++ _CONDA_ROOT=/Users/applemacbook/anaconda
++ _conda_script_is_sourced
++ sourced=0
++ '[' -n '' ']'
++ '[' -n '' ']'
++ '[' -n x ']'
++ '[' /Users/applemacbook/anaconda/bin/activate = -bash ']'
++ return 0
++ _conda_set_vars
++ '[' -n x ']'
++ _CONDA_SHELL_FLAVOR=bash
++ local script_dir
++ case "$_CONDA_SHELL_FLAVOR" in
+++ dirname /Users/applemacbook/anaconda/bin/activate
++ script_dir=/Users/applemacbook/anaconda/bin
++ '[' -z x ']'
++ cd .. /Users/applemacbook/anaconda/etc/profile.d/conda.sh
++ _conda_activate py3
-bash: _conda_activate: command not found

$ cat /Users/applemacbook/anaconda/bin/activate
+ cat /Users/applemacbook/anaconda/bin/activate
#!/bin/sh
_CONDA_ROOT="/Users/applemacbook/anaconda"
#!/bin/sh

_conda_set_vars() {
    # set _CONDA_SHELL_FLAVOR
    if [ -n "${BASH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=bash
    elif [ -n "${ZSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=zsh
    elif [ -n "${KSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=ksh
    elif [ -n "${POSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=posh
    else
        # https://unix.stackexchange.com/a/120138/92065
        local _q="$(ps -p$$ -o cmd="",comm="",fname="" 2>/dev/null | sed
's/^-//' | grep -oE '\w+' | head -n1)"
        if [ "$_q" = dash ]; then
            _CONDA_SHELL_FLAVOR=dash
        else
            (>&2 echo "Unrecognized shell.")
            return 1
        fi
    fi

    #
https://unix.stackexchange.com/questions/4650/determining-path-to-sourced-shell-script/
    local script_dir
    case "$_CONDA_SHELL_FLAVOR" in
        bash) script_dir="$(dirname "${BASH_SOURCE[0]}")";;
        zsh) script_dir="$(dirname "${(%):-%x}")";;  #
http://stackoverflow.com/a/28336473/2127762
        dash) x=$(lsof -p $$ -Fn0 | tail -1); script_dir="$(dirname
"${x#n}")";;
        *) script_dir="$(cd "$(dirname "$_")" && echo "$PWD")";;
    esac

    if [ -z "${_CONDA_ROOT+x}" ]; then
        _CONDA_ROOT="$(dirname "$script_dir")"
    fi

}

_conda_script_is_sourced() {
    # http://stackoverflow.com/a/28776166/2127762
    sourced=0
    if [ -n "${ZSH_EVAL_CONTEXT:+x}" ]; then
      case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
    elif [ -n "${KSH_VERSION:+x}" ]; then
      [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd
$(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] &&
sourced=1
    elif [ -n "${BASH_VERSION:+x}" ]; then
      [ "${BASH_SOURCE[0]}" = "$0" ] && sourced=1
    else # All other shells: examine $0 for known shell binary filenames
      # Detects `sh` and `dash`; add additional shell filenames as needed.
      case ${0##*/} in sh|dash) sourced=0;; *) sourced=1;; esac
    fi
    return $sourced
}

if ! _conda_script_is_sourced; then
    (
      >&2 echo "Error: activate must be sourced. Run 'source activate
envname'"
      >&2 echo "instead of 'activate envname'."
    )
    exit 1
fi

_conda_set_vars
. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
_conda_activate "$@"

```
Comparing from above

```
++ script_dir=/Users/applemacbook/anaconda/bin
++ '[' -z x ']'
++ cd .. /Users/applemacbook/anaconda/etc/profile.d/conda.sh
++ _conda_activate py3
-bash: _conda_activate: command not found
```

to what I get myself

```
++ script_dir=/Users/kfranz/miniconda/bin
++ '[' -z x ']'
++ . /Users/kfranz/miniconda/etc/profile.d/conda.sh
+++ _CONDA_EXE=/Users/kfranz/miniconda/bin/conda
+++ _CONDA_ROOT=/Users/kfranz/miniconda
```
Guessing this is a duplicate of https://github.com/conda/conda/issues/6639?
Maybe not.  Could you open a new terminal, and then give me the output of

    env | sort
    set -x
    source activate py3
You'll want to close the terminal down after you do the above.  Or

    set +x

to get things back to normal.

Also, can you give me the output of 

    cat /Users/applemacbook/anaconda/bin/activate
Edited by moderator. Output given fixed-width below.



Alright, I think I've got all the outputs you asked for below:
```
$ env | sort
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.XLbN3qRfIV/Render
CDPATH=.:/Users/applemacbook:/Users/applemacbook/Documents/Galvanize:/Users/applemacbook/Documents/Galvanize/immersive:/Users/applemacbook/Documents:
COLORFGBG=7;0
COLORTERM=truecolor
EDITOR=emacs
HADOOP_HOME=/usr/local/hadoop
HOME=/Users/applemacbook
ITERM_PROFILE=Default
ITERM_SESSION_ID=w0t3p0:1061674E-3BF3-4203-A297-44BAC655F9A4
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
LANG=en_US.UTF-8
LOGNAME=applemacbook
PATH=/usr/local/spark/bin:/Users/applemacbook/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PS1=$
PWD=/Users/applemacbook
PYTHONPATH=/usr/local/spark/python:
SHELL=/bin/bash
SHLVL=1
SPARK_HOME=/usr/local/spark
SPARK_LOCAL_IP=localhost
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.FwpsT72RUz/Listeners
TERM=xterm-256color
TERM_PROGRAM=iTerm.app
TERM_PROGRAM_VERSION=3.1.5
TERM_SESSION_ID=w0t3p0:1061674E-3BF3-4203-A297-44BAC655F9A4
TMPDIR=/var/folders/yp/3f96txfd1rj8m8k28xxbvpkh0000gn/T/
USER=applemacbook
VISUAL=emacs
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
_=/usr/bin/env
__CF_USER_TEXT_ENCODING=0x0:0:0

$ set -x
+ set -x

$ source activate py3
+ source activate py3
++ _CONDA_ROOT=/Users/applemacbook/anaconda
++ _conda_script_is_sourced
++ sourced=0
++ '[' -n '' ']'
++ '[' -n '' ']'
++ '[' -n x ']'
++ '[' /Users/applemacbook/anaconda/bin/activate = -bash ']'
++ return 0
++ _conda_set_vars
++ '[' -n x ']'
++ _CONDA_SHELL_FLAVOR=bash
++ local script_dir
++ case "$_CONDA_SHELL_FLAVOR" in
+++ dirname /Users/applemacbook/anaconda/bin/activate
++ script_dir=/Users/applemacbook/anaconda/bin
++ '[' -z x ']'
++ cd .. /Users/applemacbook/anaconda/etc/profile.d/conda.sh
++ _conda_activate py3
-bash: _conda_activate: command not found

$ cat /Users/applemacbook/anaconda/bin/activate
+ cat /Users/applemacbook/anaconda/bin/activate
#!/bin/sh
_CONDA_ROOT="/Users/applemacbook/anaconda"
#!/bin/sh

_conda_set_vars() {
    # set _CONDA_SHELL_FLAVOR
    if [ -n "${BASH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=bash
    elif [ -n "${ZSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=zsh
    elif [ -n "${KSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=ksh
    elif [ -n "${POSH_VERSION:+x}" ]; then
        _CONDA_SHELL_FLAVOR=posh
    else
        # https://unix.stackexchange.com/a/120138/92065
        local _q="$(ps -p$$ -o cmd="",comm="",fname="" 2>/dev/null | sed
's/^-//' | grep -oE '\w+' | head -n1)"
        if [ "$_q" = dash ]; then
            _CONDA_SHELL_FLAVOR=dash
        else
            (>&2 echo "Unrecognized shell.")
            return 1
        fi
    fi

    #
https://unix.stackexchange.com/questions/4650/determining-path-to-sourced-shell-script/
    local script_dir
    case "$_CONDA_SHELL_FLAVOR" in
        bash) script_dir="$(dirname "${BASH_SOURCE[0]}")";;
        zsh) script_dir="$(dirname "${(%):-%x}")";;  #
http://stackoverflow.com/a/28336473/2127762
        dash) x=$(lsof -p $$ -Fn0 | tail -1); script_dir="$(dirname
"${x#n}")";;
        *) script_dir="$(cd "$(dirname "$_")" && echo "$PWD")";;
    esac

    if [ -z "${_CONDA_ROOT+x}" ]; then
        _CONDA_ROOT="$(dirname "$script_dir")"
    fi

}

_conda_script_is_sourced() {
    # http://stackoverflow.com/a/28776166/2127762
    sourced=0
    if [ -n "${ZSH_EVAL_CONTEXT:+x}" ]; then
      case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
    elif [ -n "${KSH_VERSION:+x}" ]; then
      [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd
$(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] &&
sourced=1
    elif [ -n "${BASH_VERSION:+x}" ]; then
      [ "${BASH_SOURCE[0]}" = "$0" ] && sourced=1
    else # All other shells: examine $0 for known shell binary filenames
      # Detects `sh` and `dash`; add additional shell filenames as needed.
      case ${0##*/} in sh|dash) sourced=0;; *) sourced=1;; esac
    fi
    return $sourced
}

if ! _conda_script_is_sourced; then
    (
      >&2 echo "Error: activate must be sourced. Run 'source activate
envname'"
      >&2 echo "instead of 'activate envname'."
    )
    exit 1
fi

_conda_set_vars
. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
_conda_activate "$@"

```
Comparing from above

```
++ script_dir=/Users/applemacbook/anaconda/bin
++ '[' -z x ']'
++ cd .. /Users/applemacbook/anaconda/etc/profile.d/conda.sh
++ _conda_activate py3
-bash: _conda_activate: command not found
```

to what I get myself

```
++ script_dir=/Users/kfranz/miniconda/bin
++ '[' -z x ']'
++ . /Users/kfranz/miniconda/etc/profile.d/conda.sh
+++ _CONDA_EXE=/Users/kfranz/miniconda/bin/conda
+++ _CONDA_ROOT=/Users/kfranz/miniconda
```

Resolving patch:
diff --git a/conda/activate.py b/conda/activate.py
--- a/conda/activate.py
+++ b/conda/activate.py
@@ -57,10 +57,10 @@ def __init__(self, shell, arguments=None):
             self.shift_args = 0
             self.command_join = '\n'
 
-            self.unset_var_tmpl = 'unset %s'
-            self.export_var_tmpl = "export %s='%s'"
+            self.unset_var_tmpl = '\\unset %s'
+            self.export_var_tmpl = "\\export %s='%s'"
             self.set_var_tmpl = "%s='%s'"
-            self.run_script_tmpl = '. "%s"'
+            self.run_script_tmpl = '\\. "%s"'
 
         elif shell == 'csh':
             self.pathsep_join = ':'.join


Test patch:
diff --git a/tests/cli/test_activate.py b/tests/cli/test_activate.py
--- a/tests/cli/test_activate.py
+++ b/tests/cli/test_activate.py
@@ -137,12 +137,18 @@ def _format_vars(shell):
 {set} CONDARC=
 {set} CONDA_PATH_BACKUP=
 {set} PATH="{new_path}"
+{set} _CONDA_ROOT="{shellpath}"
 """
         if 'bash' in shell:
             _command_setup += "set -u\n"
 
-    command_setup = _command_setup.format(here=dirname(__file__), PYTHONPATH=shelldict['path_to'](PYTHONPATH),
-           set=shelldict["set_var"], new_path=base_path)
+    command_setup = _command_setup.format(
+        here=dirname(__file__),
+        PYTHONPATH=shelldict['path_to'](PYTHONPATH),
+        set=shelldict["set_var"],
+        new_path=base_path,
+        shellpath=join(dirname(CONDA_PACKAGE_ROOT), 'conda', 'shell')
+    )
     if shelldict["shell_suffix"] == '.bat':
         command_setup = "@echo off\n" + command_setup
 
@@ -342,51 +348,6 @@ def test_wrong_args(shell):
         assert_equals(stdout, shell_vars['base_path'], stderr)
 
 
-@pytest.mark.installed
-def test_activate_help(shell):
-    shell_vars = _format_vars(shell)
-    with TemporaryDirectory(prefix=ENVS_PREFIX, dir=dirname(__file__)) as envs:
-        if shell not in ['powershell.exe', 'cmd.exe']:
-            commands = (shell_vars['command_setup'] + """
-            "{syspath}{binpath}activate" Zanzibar
-            """).format(envs=envs, **shell_vars)
-            stdout, stderr = run_in(commands, shell)
-            assert_equals(stdout, '')
-            assert_in("activate must be sourced", stderr)
-            # assert_in("Usage: source activate ENV", stderr)
-
-        commands = (shell_vars['command_setup'] + """
-        {source} "{syspath}{binpath}activate" --help
-        """).format(envs=envs, **shell_vars)
-
-        stdout, stderr = run_in(commands, shell)
-        assert_equals(stdout, '')
-
-        if shell in ["cmd.exe", "powershell"]:
-            # assert_in("Usage: activate ENV", stderr)
-            pass
-        else:
-            # assert_in("Usage: source activate ENV", stderr)
-
-            commands = (shell_vars['command_setup'] + """
-            {syspath}{binpath}deactivate
-            """).format(envs=envs, **shell_vars)
-            stdout, stderr = run_in(commands, shell)
-            assert_equals(stdout, '')
-            assert_in("deactivate must be sourced", stderr)
-            # assert_in("Usage: source deactivate", stderr)
-
-        commands = (shell_vars['command_setup'] + """
-        {source} {syspath}{binpath}deactivate --help
-        """).format(envs=envs, **shell_vars)
-        stdout, stderr = run_in(commands, shell)
-        assert_equals(stdout, '')
-        # if shell in ["cmd.exe", "powershell"]:
-        #     assert_in("Usage: deactivate", stderr)
-        # else:
-        #     assert_in("Usage: source deactivate", stderr)
-
-
 @pytest.mark.installed
 def test_PS1_changeps1(shell):  # , bash_profile
     shell_vars = _format_vars(shell)
diff --git a/tests/test_activate.py b/tests/test_activate.py
--- a/tests/test_activate.py
+++ b/tests/test_activate.py
@@ -459,13 +459,13 @@ def test_posix_basic(self):
         new_path_parts = activator._add_prefix_to_path(self.prefix)
         assert activate_data == dals("""
         PS1='%(ps1)s'
-        export CONDA_DEFAULT_ENV='%(native_prefix)s'
-        export CONDA_PREFIX='%(native_prefix)s'
-        export CONDA_PROMPT_MODIFIER='(%(native_prefix)s) '
-        export CONDA_PYTHON_EXE='%(sys_executable)s'
-        export CONDA_SHLVL='1'
-        export PATH='%(new_path)s'
-        . "%(activate1)s"
+        \\export CONDA_DEFAULT_ENV='%(native_prefix)s'
+        \\export CONDA_PREFIX='%(native_prefix)s'
+        \\export CONDA_PROMPT_MODIFIER='(%(native_prefix)s) '
+        \\export CONDA_PYTHON_EXE='%(sys_executable)s'
+        \\export CONDA_SHLVL='1'
+        \\export PATH='%(new_path)s'
+        \\. "%(activate1)s"
         """) % {
             'converted_prefix': activator.path_conversion(self.prefix),
             'native_prefix': self.prefix,
@@ -489,12 +489,12 @@ def test_posix_basic(self):
 
             new_path_parts = activator._replace_prefix_in_path(self.prefix, self.prefix)
             assert reactivate_data == dals("""
-            . "%(deactivate1)s"
+            \\. "%(deactivate1)s"
             PS1='%(ps1)s'
-            export CONDA_PROMPT_MODIFIER='(%(native_prefix)s) '
-            export CONDA_SHLVL='1'
-            export PATH='%(new_path)s'
-            . "%(activate1)s"
+            \\export CONDA_PROMPT_MODIFIER='(%(native_prefix)s) '
+            \\export CONDA_SHLVL='1'
+            \\export PATH='%(new_path)s'
+            \\. "%(activate1)s"
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.sh')),
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.sh')),
@@ -511,14 +511,14 @@ def test_posix_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
-            . "%(deactivate1)s"
-            unset CONDA_DEFAULT_ENV
-            unset CONDA_PREFIX
-            unset CONDA_PROMPT_MODIFIER
-            unset CONDA_PYTHON_EXE
+            \\. "%(deactivate1)s"
+            \\unset CONDA_DEFAULT_ENV
+            \\unset CONDA_PREFIX
+            \\unset CONDA_PROMPT_MODIFIER
+            \\unset CONDA_PYTHON_EXE
             PS1='%(ps1)s'
-            export CONDA_SHLVL='0'
-            export PATH='%(new_path)s'
+            \\export CONDA_SHLVL='0'
+            \\export PATH='%(new_path)s'
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.sh')),