ターミナルを開いたら変な文字が…
今日、ターミナルを開いたら、突然以下のメッセージが表示された。。
実行環境と状況
macOS Mojave
- ターミナルはzshではなく、bashです。
python2.7とpython3.7が入っていて、npmのパッケージで一部python2系が必要だったので、完全に3系に移行はできない状態ということもあり、これまでいじり倒しすぎてpython環境がわけわからないことになっていたので、Anaconda消したり最近整理中で迷走中です。
とりあえずpyenvが使えないと切り替えができないので困ったという状況でした。
試したこと
エラーメッセージに書いている通り$ pyenv -initしてみる
訳がわからない…
とりあえずエラーメッセージで促された通り、 $ pyenv -init
をターミナルへ入力してみました。
出力内容はこんな感じ。
$ pyenv init
# (The below instructions are intended for common
# shell setups. See the README for more guidance
# if they don't apply and/or don't work for you.)
# Add pyenv executable to PATH and
# enable shims by adding the following
# to ~/.profile:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
# If your ~/.profile sources ~/.bashrc,
# the lines need to be inserted before the part
# that does that. See the README for another option.
# If you have ~/.bash_profile, make sure that it
# also executes the above lines -- e.g. by
# copying them there or by sourcing ~/.profile
# Load pyenv into the shell by adding
# the following to ~/.bashrc:
eval "$(pyenv init -)"
# Make sure to restart your entire logon session
# for changes to profile files to take effect.
すすめられるままにREAD MEを読んでみる
shell setups. See the README for more guidance
と、オススメされていたので、読んでみました。
README.md
READMEによると以下の通り記述がありました。
MacOS note:
If you installed Pyenv with Homebrew, you don't need to add the PYENV_ROOT= and PATH= lines. You also don't need to add commands into ~/.profile if your shell doesn't use it.
私はmacで、bashで、Homebrewからpyenvを入れました。
どうやらそういう人は以下の2行は入力の必要がないらしいです。
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
入力する必要があるのは以下のみの模様。
echo 'eval "$(pyenv init --path)"' >> ~/.profile
いざ入力してみる
.bashrc向け、.profile向け、.zprofile向けの編集方法がREADMEには書かれていましたが、とりあえず私はzshではなくbashだし、macでHomeberwからpyenvを入れました。
.profileと.bash_profileなど、どんなファイルがあるのか$ ls -a
で見てみたところ、.profileは存在せず、.bash_profileがあり、$ vi .bash_profile
で開いて見たところ以下の記述がありました。
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
これは自分で編集したものではないです。
おそらくこの記述、Homebrewでpyenvを入れた際に自動で書き込まれたのではないかと。。。
だからmacでHomebrewから入れた人は以下の2行は入れる必要がない(=すでに入っているから追記する必要がないよ)ということなのでは、と思いました。
READMEには.profileファイルに書き込む様に案内されていましたが、これを行うためには.profileファイルを作って、そこに記述しなければいけませんし、macだと省略していいよと言われている2行も入れないといけない様な気がします。
そのため、3行目の記述を以下に変更しました。
eval "$(pyenv init --path)"
最後に以下のコマンドで、入力内容を反映させて…
$ source .bash_profile
そしたら無事に冒頭のエラーメッセージが出なくなり、mac標準のPython2系が使われず、pyenvで入れたPython3系が無事にGlobalで使える様になりました。
.bash_profileとか.profileとか、わからないので調べてみようと思います。。