在新的xterm终端中激活conda环境

时间:2019-12-18 16:20:50

标签: bash conda xterm

如果我跑步

xterm -hold

并在新终端中输入

conda activate my_environment

conda环境“ my_environment”确实已激活。

但是,当使用-e标志传递此命令时,它不起作用:

xterm -hold -e "conda activate my_environment"

它返回以下错误消息:

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.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

那么,如何使用xterm做到这一点?还是应该使用其他类型的外部端子?

1 个答案:

答案 0 :(得分:1)

背景

conda activate命令是一个外壳函数,在外壳初始化期间定义。 conda init将代码添加到初始化文件(例如.bash_profile),以运行定义conda activate shell函数的脚本。

解决方案

可能的解决方法:xterm个选项

-c参数与xterm一起使用时,它将不再运行初始化脚本。因此,conda activate从未定义。对于bash,有-l告诉它运行初始化文件。我期望xterm的-ls参数会触发类似的行为,但是它对我不起作用。也许更熟悉的人可以将您指向正确的标志。

手动运行Conda脚本

否则,您只需要自己运行Conda脚本(假设它是bash版本)。这些都可以使用:

xterm -hold -e ". /path/to/miniconda3/etc/profile.d/conda.sh && conda activate my_environment && which python"

xterm -hold -e "$(conda shell.bash hook) && conda activate my_environment && which python"

仅包含which python,以表明您正在激活环境。

Conda Run

另一个选项是conda run,它可以在环境下自动执行命令。以下内容与我在上一节中所做的等效,但是不必知道我在哪个shell中运行:

xterm -hold -e "conda run -n my_environment which python"

请注意,此功能仍在开发中。就个人而言,我发现它对于在特定环境中运行简单脚本很有用,而且没有遇到问题。

相关问题