今天我花了几个小时为“microsamba”设置 direnv。
由于安装减少,没有
acticate
脚本可供源,我在设置正确的布局时遇到了麻烦。
比如说
layout_micromamba() {
export MAMBA_HOME="${HOME}/Data/micromamba"
export MAMBA_EXE="${HOME}/.local/bin/micromamba"
export MAMBA_ROOT_PREFIX='/home/abu/Data/micromamba'
local my_shell=$(basename ${SHELL})
local env_name="$1"
__mamba_setup="$("${MAMBA_EXE}" shell hook --shell "${my_shell}" --root-prefix "${MAMBA_ROOT_PREFIX}" 2> /dev/null)"
__mamba_activate="$("${MAMBA_EXE}" activate "${env_name}")"
eval " ${__mamba_setup}"
if [ -n "$1" ]; then
# Explicit environment name from layout command.
eval activate ${env_name}
elif (grep -q name: environment.yml); then
# Detect environment name from `environment.yml` file in `.envrc` directory
eval "$mamba_hook; micromamba activate $(grep name: environment.yml | sed -e 's/name: //')"
else
(>&2 echo No environment specified);
exit 1;
fi;
unset __mamba_setup
unset __mamba_activate
}
输出错误
direnv: loading ~/test-env/.envrc
action: hool not in {init,deinit,reinit,hook,activate,deactivate,reactivate}
Run with --help for more information.
'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
direnv: export ~PATH
我尝试了多种方法,但没有成功。
几个小时后我找到了解决方案。
microsamba activate
必须与 shell 函数 microsamba
相关,该函数在调用 micromamba shell hook
命令后生成。由于我在网上找不到解决方案,我想分享 direnv
的正确布局。将此代码放入您的 ~/.config/direnv/direnvrc
文件中:
#
# micromamba
#
# touch a '.envrc' file in your project directory and insert the following code in it:
# layout micromamba <your-environment-name>
layout_micromamba() {
# adjust the target to your installation of the BINARY micromamba
export MAMBA_EXE="${HOME}/.local/bin/micromamba"
# adjust the target to the installation prefix
export MAMBA_ROOT_PREFIX="${HOME}/micromamba"
# find the shell in use
local my_shell=$(basename ${SHELL})
local env_name="$1"
__mamba_setup="$("${MAMBA_EXE}" shell hook --shell "${my_shell}" --root-prefix "${MAMBA_ROOT_PREFIX}" 2> /dev/null)"
eval " ${__mamba_setup}"
if [ -n "$1" ]; then
# Explicit environment name from layout command
# DO NOT USE $MAMBA_EXE , instead the shell function 'micromamba' which got generated after '__mamba_setup'.
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;
unset __mamba_setup
}