静默炮弹警告

2024-07-05 15:59:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在Bash脚本中激活Conda环境时,例如:

#!/bin/bash
conda activate ./myenv
which python
python do_stuff.py

激活行打印出长警告:

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'.

我该如何消除这个警告?如果我运行它所建议的conda init bash命令,那么Conda会在默认情况下为所有Bash shell激活我的环境,这绝对不是我想要的。这是Conda中的默认配置,在它破坏了整个系统的所有功能之后,我明确地关闭了它。我希望Conda的功能类似于virtualenv,在virtualenv中我有一个包含环境的文件夹,并且只在特定应用程序需要时才激活它。在任何情况下,我都不希望系统中任何地方的Bash运行实例都激活Conda环境

我不知道为什么它会给我一个CommandNotFoundError,因为否则Conda会用Python和其他特定于环境的资源的正确路径填充bashshell,其他的一切都正常工作


Tags: andtobash警告your环境init系统
2条回答

初始化conda之后,默认情况下,它将在您打开的每个新shell会话上自动激活基本环境。如果要关闭此行为,只需运行一次:

conda config  set auto_activate_base false

在脚本中预先编写以下代码

eval "$(command conda 'shell.bash' 'hook' 2> /dev/null)"

这只在脚本中初始化conda

相关问题 更多 >