【问题标题】:JupyterLab: Unable to switch environments in Terminal WindowJupyterLab:无法在终端窗口中切换环境
【发布时间】:2022-01-22 20:55:30
【问题描述】:

我在 MacOS 上运行 JupyterLab,由于某种原因,在尝试切换虚拟环境时出现以下错误:

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.

我可以在 JupyterLab 终端中执行其他 conda 命令,例如 bash-3.2$ conda env list 而不会出错。我能够在 iTerm 的 JupyterLab 终端之外执行 bash-3.2$ conda activate yhoo 而不会出错。因此,这似乎是通过 JupyterLab 运行终端的问题。感谢任何指导。

【问题讨论】:

    标签: python-3.x terminal virtualenv conda jupyter-lab


    【解决方案1】:

    Conda activate 命令是一个 shell 命令,通常通过获取用户的 shell 资源文件或配置文件(例如,~/.bash_profile)来定义。 Jupyter Terminado 默认不以登录模式启动,即它不获取配置文件。因此,它没有定义conda activate,也没有任何conda init'ing 可以解决它。

    快速修复

    对于 ad hoc 修复,只需获取您的文件

    source ~/.bash_profile
    

    这假设conda init 至少运行过一次,这是 MacOS。

    永久修复

    Jupyter 具有定义终端启动时使用的 shell 命令的设置。如果 Jupyter 安装在 Conda 环境中,比如foo,那么设置文件将位于:

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

    并对其进行编辑以包含设置

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

    或者,您可以使用:

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

    并添加设置

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

    类似于what is noted on the Jupyter Discourse

    【讨论】:

    • 谢谢。临时修复有效,但不幸的是永久修复没有。我输入了两个命令并收到相同的错误:$ (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'`
    • $ (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 “$”后没有空格。 $(…) 运行命令并内联返回结果。抱歉,如果不清楚,但我只是将其作为一般 find 文件的一种方式,而不是执行任何操作的命令。如果你知道你的 Jupyter 安装在哪里,只需在 GUI 中查找文件
    • 再次感谢您的回复。所以我在 iterm2 上运行 bash。 “$”后面没有空格,只是iterm自然缩进。话虽如此,我能够手动 cd.. 到 ~/opt/miniconda3/envs/foo/etc/jupyter/ 并发现它包含三个文件:jupyter_notebook_config.dnbconfigjupyter_server_config.d。至少对我来说,jupyter_notebook_config.py 文件位于~/.jupyter/。你知道这是个问题还是我应该只编辑~/.jupyter 中的文件?
    • @jgg 好的,感谢您的澄清。如果不存在配置文件,您可以简单地创建一个(其中任何一个)并添加提到的行。不确定~/.jupyter/,但可能可以将它添加到那里,它也会被拾取。
    猜你喜欢
    • 2011-08-05
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多