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

问题描述 投票:0回答:1

如果我跑步

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完成此操作?还是应该使用其他类型的外部端子?

bash conda xterm
1个回答
0
投票

背景

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"
© www.soinside.com 2019 - 2024. All rights reserved.