我正在尝试使用 direnv 自动激活 conda/mamba 环境,但我遇到了一个问题,这似乎是由于我对 bash 脚本如何使用环境变量缺乏了解。
我的 shell 是 Fish,但 direnv 是基于 bash 的。我不认为我的外壳是鱼对这个问题有任何影响,因为我为鱼运行了适当的 direnv 钩子。
我的.direnvrc
文件如下:
layout_mamba() {
local MAMBA_EXE="${HOME}/bin/micromamba" # Make sure this points to path of your micromamba executable
# initialize micromamba
eval "$(${MAMBA_EXE} shell hook --shell=bash)"
if [ -n "$1" ]; then
# Explicit environment name from layout command.
local env_name="$1"
micromamba activate ${env_name}
elif (grep -q name: environment.yml); then
# Detect environment name from `environment.yml` file in `.envrc` directory
micromamba activate `grep name: environment.yml | sed -e 's/name: //'`
else
(>&2 echo No environment specified);
exit 1;
fi;
}
它按原样工作,但请注意,在我的声明中,我直接使用了but 其余调用eval
的值。我尝试用 $MAMBA_EXE 替换 micromamba 的所有尝试都失败了。$MAMBA_EXE
micromamba
有什么建议可以让我的内容.direnvrc
更通用吗?
编辑:例如,如果我将第 8 行从 改为 ,我会micromamba activate ${env_name}
得到${MAMBA_EXE} activate ${env_name}
:
direnv: loading ~/test-env/.envrc
'micromamba' is running as a subprocess and can't modify the parent shell.
Thus you must initialize your shell before using activate and deactivate.
To initialize the current bash shell, run:
$ eval "$(micromamba shell hook --shell=bash)"
and then activate or deactivate with:
$ micromamba activate
To automatically initialize all future (bash) shells, run:
$ micromamba shell init --shell=bash --prefix=~/micromamba
Supported shells are {bash, zsh, csh, xonsh, cmd.exe, powershell, fish}.
critical libmamba Shell not initialized
鱼不让我跑${MAMBA_EXE} shell hook --shell=bash
,但跑步$MAMBA_EXE shell hook --shell=bash
会产生:
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
__mamba_exe() (
"$MAMBA_EXE" "$@"
)
__mamba_hashr() {
if [ -n "${ZSH_VERSION:+x}" ]; then
\rehash
elif [ -n "${POSH_VERSION:+x}" ]; then
: # pass
else
\hash -r
fi
}
__mamba_activate() {
\local ask_conda
ask_conda="$(PS1="${PS1:-}" __mamba_exe shell --shell bash "$@")" || \return
\eval "$ask_conda"
__mamba_hashr
}
__mamba_reactivate() {
\local ask_conda
ask_conda="$(PS1="${PS1:-}" __mamba_exe shell --shell bash reactivate)" || \return
\eval "$ask_conda"
__mamba_hashr
}
micromamba() {
\local cmd="${1-__missing__}"
case "$cmd" in
activate|deactivate)
__mamba_activate "$@"
;;
install|update|upgrade|remove|uninstall)
__mamba_exe "$@" || \return
__mamba_reactivate
;;
self-update)
__mamba_exe "$@" || \return
# remove leftover backup file on Windows
if [ -f "$MAMBA_EXE.bkup" ]; then
rm -f $MAMBA_EXE.bkup
fi
;;
*)
__mamba_exe "$@"
;;
esac
}
if [ -z "${CONDA_SHLVL+x}" ]; then
\export CONDA_SHLVL=0
# In dev-mode MAMBA_EXE is python.exe and on Windows
# it is in a different relative location to condabin.
if [ -n "${_CE_CONDA+x}" ] && [ -n "${WINDIR+x}" ]; then
PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}"
else
PATH="${MAMBA_ROOT_PREFIX}/condabin:${PATH}"
fi
\export PATH
# We're not allowing PS1 to be unbound. It must at least be set.
# However, we're not exporting it, which can cause problems when starting a second shell
# via a first shell (i.e. starting zsh from bash).
if [ -z "${PS1+x}" ]; then
PS1=
fi
fi
if [ -n "${ZSH_VERSION:+x}" ]; then
if ! command -v compinit > /dev/null; then
autoload -U +X compinit && if [[ "${ZSH_DISABLE_COMPFIX-}" = true ]]; then
compinit -u
else
compinit
fi
fi
autoload -U +X bashcompinit && bashcompinit
_umamba_zsh_completions()
{
COMPREPLY=($(__mamba_exe completer "${(@s: :)${(@s: :)COMP_LINE}:1}"))
}
complete -o default -F _umamba_zsh_completions micromamba
fi
if [ -n "${BASH_VERSION:+x}" ]; then
_umamba_bash_completions()
{
COMPREPLY=($(__mamba_exe completer "${COMP_WORDS[@]:1}"))
}
complete -o default -F _umamba_bash_completions micromamba
fi