2

I'm running JupyterLab on MacOS and for some reason I get the following error when trying to switch virtual environments:

bash-3.2$ conda activate yhoo

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

I'm able to execute other conda commands in the JupyterLab Terminal such as bash-3.2$ conda env list without error. I'm able to execute bash-3.2$ conda activate yhoo outside the JupyterLab Terminal in iTerm without error. Therefore, it seems this is an issue with running Terminal via JupyterLab. Grateful for any guidance.

jgg
  • 791
  • 4
  • 17

1 Answers1

1

The Conda activate command is a shell command that is typically defined through sourcing the user's shell resource file or profile (e.g., ~/.bash_profile). The Jupyter Terminado defaults to launching not in login mode, i.e., it doesn't source the profile. Hence, it doesn't define conda activate, and no amount of conda init'ing is going to resolve it.

Quick Fix

For ad hoc fix, just source your file

source ~/.bash_profile

This assumes conda init has been run at least once and this is MacOS.

Permanent Fix

Jupyter has settings to define the shell command that Terminado launches with. If Jupyter is installed in a Conda environment, say foo, then the settings file will be at:

$(conda run -n foo echo \$CONDA_PREFIX)/etc/jupyter/jupyter_notebook_config.json

And edit it to include the setting

{
  "NotebookApp": {
    "terminado_settings": {
      "shell_command": ["/bin/bash", "-l"]
    }
  }
}

Alternatively, you can use:

$(conda run -n foo echo \$CONDA_PREFIX)/etc/jupyter/jupyter_notebook_config.py

and add the setting

c.NotebookApp.terminado_settings = { "shell_command": ["/bin/bash", "-l"] }

similar to what is noted on the Jupyter Discourse.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Thank you. The temporary fix worked but unfortunately the permanent fix did not. I entered both commands and received the same error: `$ (conda run -n base echo \$CONDA_PREFIX)/etc/jupyter/jupyter_notebook_config.json -bash: syntax error near unexpected token `/etc/jupyter/jupyter_notebook_config.json'` – jgg Jan 25 '22 at 04:31
  • `$ (conda run -n yhoo echo \$CONDA_PREFIX)/etc/jupyter/jupyter_notebook_config.py -bash: syntax error near unexpected token `/etc/jupyter/jupyter_notebook_config.py'` – jgg Jan 25 '22 at 04:33
  • @jgg no space after the “$”. The `$(…)` runs a command and returns the result inline. Sorry if it wasn’t clear, but I’m just giving that as a way to generically *find* the file, not a command that does anything. If you know where your Jupyter installation is, just track down the file in GUI – merv Jan 25 '22 at 05:56
  • 1
    thanks again for the response. so I'm running bash on iterm2. there's not a space after the "$", it's just that iterm naturally indents that way. Having said that, I was able to manually cd.. to `~/opt/miniconda3/envs/foo/etc/jupyter/` and found that it contained three files: `jupyter_notebook_config.d`, `nbconfig` & `jupyter_server_config.d`. At least for me, the `jupyter_notebook_config.py` file was located in `~/.jupyter/`. Do you know if that's a problem or should I just edit the file in `~/.jupyter`? – jgg Jan 26 '22 at 00:14
  • @jgg okay, thanks for the clarification. If no config file exists already, you can simply create one (either of them) and add the line(s) mentioned. Not sure about the `~/.jupyter/`, but likely can add it there and it would be picked up as well. – merv Jan 26 '22 at 02:24