2
1

More than 3 years have passed since last update.

Pythonのglobalでのバージョンが切り替えられなかった時の対処法

Last updated at Posted at 2021-05-30

環境

  • macOS BigSur 11.2.3
  • shell:zsh

問題

macにデフォルトで入っていたpython2.7xから3xへ変更したかったけど、変更できなかった。

切り替え前のバージョン

$ python --version
Python 2.7.10

Homebrewでpyenvをインストール

$ brew install pyenv

pyenvの設定

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc
$ source ~/.zshrc

pythonをインストール(今回は3.8.8をインストール)

$ pyenv install 3.8.8

globalのバージョンも変更

$ pyenv global 3.8.8

が、確認してみると

$ pyenv global 3.8.8
Python 3.7.10

どうやらpyenv アップデートによって、path の設定が変更になったようなので、

$ 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 and ~/.zprofile:

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

# Load pyenv into the shell by adding
# the following to ~/.zshrc:

eval "$(pyenv init -)"

# Make sure to restart your entire logon session
# for changes to profile files to take effect.

記述されているとおり.zprofile、.zshrcにそれぞれ記述する

$ open ~/.zprofile 
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

保存して、同じくzshrcにも追記する

$ open ~/.zshrc
eval "$(pyenv init -)"

保存し、最後に反映させる

source ~/.zshrc       

もう一度バージョンを確認

$ python --version
Python 3.8.8

変更できたみたい!

参考記事

pyenvでPythonのバージョンを切り替えられない場合の対処法+
Python pyenv アップデートでpathの設定が変更

初投稿でした

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1