anaconda + screen で activate 時にエラー発生
始めに
私は screen 環境をよく使うのですが、新しい環境で anaconda を初めて使うときなどによく発生するエラーとして以下があります。
何度もハマっている(のに忘れる)ため、備忘のため書いておこうと思います。
エラーについて
どういう時に発生するかと言うと、
1. anaconda を install します。
参考:https://entre-temps.hatenablog.com/entry/setup_machinelearning_env
2. 新しい env を作ります。
$ conda create -n py36_base python=3.6 $ conda info -e # conda environments: # base * /anaconda3 py36_base /anaconda3/envs/py36_base
3. screen で使おうと、起動し conda activate py36_base
するとエラーが発生します。
~ $ conda activate py36_base 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'.
原因
何が原因でこれが起こるかというと、要するに .bash_profile を screen はデフォルトではロードしないのです。
.bash_profile には下記の anaconda の初期設定スニペットがインストール時に自動的に追記されます。
そのため、ここがロードされないと正常に環境変数などが設定されないことになり、先程のエラーに繋がります。
# added by Anaconda3 2019.10 installer # >>> conda init >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$(CONDA_REPORT_ERRORS=false '/opt/anaconda3/bin/conda' shell.bash hook 2> /dev/null)" if [ $? -eq 0 ]; then \eval "$__conda_setup" else if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then # . "/opt/anaconda3/etc/profile.d/conda.sh" # commented out by conda initialize CONDA_CHANGEPS1=false conda activate base else \export PATH="/opt/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda init <<< # >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then . "/opt/anaconda3/etc/profile.d/conda.sh" else export PATH="/opt/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda initialize <<<
解決方法
至極単純で、.screenrc に次の一行を加え、screen 起動時に .bash_profile を読むように設定すれば解決します。 https://serverfault.com/questions/226783/how-to-tell-gnu-screen-to-run-bash-profile-in-each-new-window/226788
shell -$SHELL
以上です。