jgg
jgg

Reputation: 831

JupyterLab: Unable to switch environments in Terminal Window

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.

Upvotes: 2

Views: 633

Answers (1)

merv
merv

Reputation: 77098

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.

Upvotes: 1

Related Questions