calendarcodediamondfacebookfingerglobalgoogleplushatenahomepagetopplainpocketrssservicetwitterwordpresshome2searchfoldernext-arrowback-arrowfirst-arrowlast-arrow

pyenv の使い方: Python をインストールした後でバージョンが適用されない場合の対処法(Mac / Windows)

Mac に Pythonをインストールして、Python3 に切り替えようとしたときに、バージョンが適用されていない場合の対処法。pyenv を使うと、1 台のパソコンで複数バージョンの python 環境を簡単に切り替えることができる。

エンジニア速報は Twitter の@commteで配信しています。

Sponsored Link

pyenv

pyenv は、1 台のパソコンで複数バージョンの python 環境を使えるようになります。

特徴

  • ユーザーごとにグローバル Python バージョンを変更
  • Python バージョンを環境変数で上書きできるようにする
  • プロジェクトごとの Python バージョンのサポートを提供
  • 一度に複数のバージョンの Python からコマンドを検索
  • Python 自体に依存する
  • virtualenvを管理する

pyenv

pyenv インストール

brew update brew install pyenv

バージョン確認

pyenv -v

インストール可能な python のバージョン一覧

pyenv install -l

Python インストール

3.9.6 を選択しインストール。

pyenv install 3.9.6

バージョン確認

pyenv versions 3.9.6

Python3 に切り替え

pyenv global 3.9.6

バージョンの確認

確認するが、バージョンが適用されていない。

python -V Python 2.7.16 which python /usr/bin/python which python3 /usr/local/bin/python3

バージョンが適用されない場合の対処法

公式ドキュメント

pyenv/pyenv: Simple Python version management

「pyenv init」コマンドを使うと python の実行パスを切り替えることができる。

pyenv init

実行すると「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.

スクリプトファイルに追記

現在使っているシェルを調べる

echo $SHELL /bin/zsh

zsh を使っている場合

.zshrc に追加

eval "$(pyenv init -)"

.zprofile に追加

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

反映

source ~/.zprofile

.bash_prifile に追加する場合

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

再確認

python -V Python 3.9.6

無事適用されました。

Python おすすめ本

スポンサード リンク

Comments

Leave a Comment

コメントする

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください