【问题标题】:conda activate command not working on macconda activate 命令在 mac 上不起作用
【发布时间】:2020-12-06 23:02:25
【问题描述】:

我有 miniconda 4.8.3 + MacOS Catalina 10.15。我可以在终端手动激活 conda 环境并启动一个 spyder 会话。

$ ~/miniconda3/bin/conda activate py3
$ ~/miniconda3/bin/conda info | grep "active environment"
$ spyder &

当我将上述内容放入脚本时,run_spyder.sh 无法正常工作,并抱怨“CommandNotFoundError:您的 shell 未正确配置为使用 'conda activate'。”

#!/bin/bash
# run_spyder.sh
~/miniconda3/bin/conda activate py3
~/miniconda3/bin/conda info | grep "active environment"  # still print base
# spyder &

我尝试了bash -i ./run_spyder.shsource ./run_spyder.sh 或添加~/miniconda3/bin/conda init bash 之类的替代方法,但都不起作用。

shell还是bash,没有.bashrc,在.bash_profile这个是miniconda安装自动生成的脚本

# .bashrc_profile
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('~/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"

if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "~/miniconda3/etc/profile.d/conda.sh" ]; then
        . "~/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="~/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

【问题讨论】:

  • zsh 现在是 macos 上的默认 shell。你可以试试conda init zsh吗?
  • ~/miniconda3/bin/conda init zsh 不会改变结果

标签: macos conda


【解决方案1】:

conda activate 函数是一个 shell 函数,通常在会话开始时在 shell 的初始化文件中定义(例如,在 .bash_profile 中)。 conda init 函数只是将代码添加到此类初始化文件中,但不会实际获取它添加的代码。希望这可以澄清问题(和 cmets)中尝试的困难。

请尝试直接采购 Conda 使用的代码。比如:

#!/bin/bash
source ~/miniconda3/etc/profile.d/conda.sh
conda activate py3
conda info | grep "active environment"
spyder &

另一种选择是让bash(或zsh)会话在登录模式下启动(即为当前用户运行初始化文件)。

#!/usr/bin/env bash -l
conda deactivate    # <- may not be needed, but didn't work for me without
conda activate py3
conda info | grep "active environment"
spyder &

但是,请注意,在后一种情况下,我发现我需要首先包含 conda deactivate,以便 conda activate 正确地优先考虑 PATH 上的环境中的 Python。

【讨论】:

  • @fivelements 您能否更具体地说明它们是如何失败的?这两种方法都适用于 osx-64 平台,但我正在通过加载特定于环境的包进行验证,例如 python -c 'import pymc3; print(pymc3.__spec__)',而不是启动 Spyder。另外,我只是使用./run_spyder.sh 运行脚本(即,没有bashsource),这让shebang 标头完成了确定如何运行脚本的工作。
  • 我启动了一个终端(即 bash),然后 .bash_profile 被执行,我可以在提示中看到(base)。然后我运行./run_spyder.sh。我遇到了和以前一样的错误CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
相关资源
最近更新 更多