bash - 在 Travis CI 上激活 conda
问题描述
我正在使用 conda 4.6.8 在 Travis CI 的 conda env 中测试 python 包。我想用我的 Travis CI 配置中的新命令source activate ENVNAME
替换我的旧行。如果我在 Travis 上运行它:conda activate ENVNAME
>>> conda update -n base conda
>>> conda init
no change /home/travis/miniconda/condabin/conda
no change /home/travis/miniconda/bin/conda
no change /home/travis/miniconda/bin/conda-env
no change /home/travis/miniconda/bin/activate
no change /home/travis/miniconda/bin/deactivate
no change /home/travis/miniconda/etc/profile.d/conda.sh
no change /home/travis/miniconda/etc/fish/conf.d/conda.fish
no change /home/travis/miniconda/shell/condabin/Conda.psm1
no change /home/travis/miniconda/shell/condabin/conda-hook.ps1
no change /home/travis/miniconda/lib/python3.7/site-packages/xonsh/conda.xsh
no change /home/travis/miniconda/etc/profile.d/conda.csh
modified /home/travis/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
如何在 Travis 上“关闭并重新打开”我的 shell?因为否则我无法激活我的 conda 环境:
>>> conda create -n TEST package_names
>>> conda activate TEST
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'.
The command "conda activate TEST" failed and exited with 1 during .
Your build has been stopped.
解决方案
不确定它目前是否受支持,因为官方文档仍在source
travis.yml 中使用。
做什么conda init
?
这个新命令应该协调用户设置他们的 shell 以便能够调用conda activate
.
实际上,如果您运行,conda init --dry-run --verbose
您会看到它尝试conda.sh
从您的来源~/.bashrc
(假设您正在运行 Bash,来自您问题中提到的信息)。
并将conda.sh
定义一个conda()
函数,该函数将捕获其中的一些命令activate
并deactivate
分派到$CONDA_EXE
:
conda() {
if [ "$#" -lt 1 ]; then
"$CONDA_EXE"
else
\local cmd="$1"
shift
case "$cmd" in
activate|deactivate)
__conda_activate "$cmd" "$@"
;;
install|update|upgrade|remove|uninstall)
"$CONDA_EXE" "$cmd" "$@" && __conda_reactivate
;;
*) "$CONDA_EXE" "$cmd" "$@" ;;
esac
fi
}
因此,除非在本地 shell 中定义了此函数,否则您将无法调用conda activate
.
提示解决方案?(未针对 Travis CI 进行测试)
我可以建议的唯一提示是尝试source $(conda info --root)/etc/profile.d/conda.sh
然后conda activate
。conda init
这应该与假设您使用 Bourne shell 衍生物大致相同。
因为csh
有$(conda info --root)/etc/profile.d/conda.csh
,因为fish
有$(conda info --root)/etc/fish/conf.d/conda.fish
注意:虽然未针对 Travis CI 进行测试,但此解决方案适用于我的 bash。当然,应该在PATH
for中找到 conda 可执行文件conda info --root
才能正常工作。