direnvと環境変数

direnvと環境変数

conda / mamba環境を自動的に有効にするためにdirenvを使用しようとしていますが、bashスクリプトが環境変数をどのように使用するかを理解していないため、問題が発生しているようです。

私のシェルはフィッシュですが、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;
}

そのまま動作しますが、私の宣言では、eval残りの呼び出しに直接値を使用しています。 micromambaを$ MAMBA_EXEに置き換えようとするすべての試みは失敗しました。$MAMBA_EXEmicromamba

私のコンテンツをより多様にする方法について提案がありますか.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

ベストアンサー1

おすすめ記事