LoginSignup
0
0

More than 1 year has passed since last update.

pyenvによるPythonのバージョン変更方法 for Mac

Last updated at Posted at 2022-01-15

参考

概要

Pythonのバージョン切り替え方法について

内容

MacでPythonのバージョンを切り替える場合はpyenvを使用する

インストール

pyenvのインストール

% brew update
% brew install pyenv

シェルの環境設定
(zshを使用している場合)

% echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
% echo 'eval "$(pyenv init -)"' >> ~/.zshrc

使用方法

インストール可能なバージョンの確認

% pyenv install --list

指定したバージョンのインストール
(今回はバージョン3.7.12をインストール)

% pyenv install 3.7.12

インストールしたバージョンの確認

% pyenv versions
* system (set by /Users/username/.pyenv/version)
  3.7.12

systemは元々入っているPython
3.7.12が今回インストールしたバージョン
*(アスタリスク)がついているバージョンが現在適用されている

バージョン切り替え

% pyenv shell 3.7.12

バージョンの確認

% pyenv versions
  system
* 3.7.12 (set by PYENV_VERSION environment variable)

もとに戻すときは

% pyenv shell --unset
* system (set by /Users/username/.pyenv/version)
  3.7.12

補足

Macにはデフォルトでpython2が入っておりコマンド"python ~"ではpython2.xが実行され
python 3.xを実行するには"python3 ~"で実行する必要がある。

しかし、pyenvで変更されるのはコマンド"python"の方なのでバージョンを変更したpythonを実行する際は"python ~"で実行する必要がある。
(コマンド"python3"は変更されない)

  • バージョン変更前
% python -V
Python 2.7.16
% python3 -V
Python 3.8.5
  • バージョン変更後
% pyenv shell 3.7.12

% python -V
Python 3.7.12

% python3 -V
Python 3.8.5

以上

0
0
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
0
0