Edited at

macOS上のzshでpyenvにAnacondaを入れたときに,conda activateが使えない場合


はじめに

これまでAnacondaを使っていなかったが,ようやく導入することとした.しかし,homebrewの環境を壊さないためにpyenv上にAnacondaを入れたため,少し面倒な事になってしまった.


conda activateが使えない

conda activateを使うと,以下のようなエラーが表示される.


zsh

bitpositive@MacBook-Pro-13 in ~/

$ conda activate py27

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'.


実行しろと言われた"conda init zsh"もエラーとなる.


zsh

bitpositive@MacBook-Pro-13 in ~/

$ conda init zsh

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

Traceback (most recent call last):
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/exceptions.py", line 1002, in __call__
return func(*args, **kwargs)
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/cli/main.py", line 84, in _main
exit_code = do_call(args, p)
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 82, in do_call
exit_code = getattr(module, func_name)(args, parser)
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/cli/main_init.py", line 52, in execute
anaconda_prompt)
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/core/initialize.py", line 106, in initialize
run_plan(plan2)
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/core/initialize.py", line 567, in run_plan
result = globals()[step['function']](*step.get('args', ()), **step.get('kwargs', {}))
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/conda/core/initialize.py", line 1039, in init_sh_user
rc_content = fh.read()
File "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position 2: invalid start byte

`$ /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/conda init zsh`

-----------------
省略
-----------------

An unexpected error has occurred. Conda has prepared the above report.

If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers?

[y/N]:

No report sent. To permanently opt-out, use

$ conda config --set report_errors false



解決方法

こんなでいいのかは不明だが,以下の方法で解決した.

一度Bash向けに初期化を行い,.bash_profileに自動的に書き込まれたスクリプトを.zprofileにコピーする


condaの初期化

以下を実行して,condaをBash向けにinitializeする.


zsh

bitpositive@MacBook-Pro-13 in ~/

$ conda init bash
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/condabin/conda
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/conda
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/conda-env
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/activate
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/deactivate
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.sh
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/fish/conf.d/conda.fish
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/shell/condabin/Conda.psm1
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/shell/condabin/conda-hook.ps1
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/lib/python3.7/site-packages/xonsh/conda.xsh
no change /Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.csh
modified /Users/bitpositive/.bash_profile

==> For changes to take effect, close and re-open your current shell. <==



スクリプトを.zprofileにコピー

.bash_profileを開き,以下のような部分をコピーする.


.bash_profile

# >>> conda initialize >>>

# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if
[ -f "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.sh" ]; then
. "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.sh"
else
export PATH="/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<

それを.zprofileにヤンクする.


.zprofile

export PYENV_ROOT="$HOME/.pyenv"

export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if
[ -f "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.sh" ]; then
. "/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/etc/profile.d/conda.sh"
else
export PATH="/Users/bitpositive/.pyenv/versions/anaconda3-2018.12/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<



変更の適用

以下を実行する.


zsh

bitpositive@MacBook-Pro-13 in ~/

$ source .zprofile
(base) bitpositive@MacBook-Pro-13 in ~/
$

"(base)"が表示され,Anacondaのenvironmentが使えていることがわかる.


conda activateを使う

conda activateが使えることを確認する.


zsh

(base) bitpositive@MacBook-Pro-13 in ~/

$ python --version
Python 3.7.1
(base) bitpositive@MacBook-Pro-13 in ~/
$ conda activate py27
(py27) bitpositive@MacBook-Pro-13 in ~/
$ python --version
Python 2.7.15 :: Anaconda, Inc.
(py27) bitpositive@MacBook-Pro-13 in ~/
$ conda deactivate
(base) bitpositive@MacBook-Pro-13 in ~/
$

以上.