Pythonのバージョン管理をしてみた(pyenvを使ってみた)

Python

pyenvとは

Pythonのバージョン管理を行うことができるツール

GitHub - pyenv/pyenv: Simple Python version management
Simple Python version management. Contribute to pyenv/pyenv development by creating an account on GitHub.

インストール

Macの人はHomebrewでできる

brew update
brew install pyenv

パスの編集も必要(Zshの例(詳細はREADMEに書かれている))

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

使い方

コマンドの詳細はここで確認できる

インストール済みのバージョンを確認する

% pyenv versions
* system (set by /Users/xxxxx/.pyenv/version)
  3.7.16

インストール可能なバージョンを確認する

% pyenv install -l
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
 :

バージョンをインストールする

% pyenv install 3.7.16
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.16.tar.xz...
-> https://www.python.org/ftp/python/3.7.16/Python-3.7.16.tar.xz
Installing Python-3.7.16...
patching file 'Doc/library/ctypes.rst'
patching file 'Lib/test/test_unicode.py'
patching file 'Modules/_ctypes/_ctypes.c'
patching file 'Modules/_ctypes/callproc.c'
patching file 'Modules/_ctypes/ctypes.h'
patching file setup.py
patching file 'Misc/NEWS.d/next/Core and Builtins/2020-06-30-04-44-29.bpo-41100.PJwA6F.rst'
patching file 'Modules/_decimal/libmpdec/mpdecimal.h'
patching file setup.py
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.16 to /Users/kentaro/.pyenv/versions/3.7.16

バージョンを変更する

バージョン名が ~/.pyenv/version ファイルに書き込まれることで、全体のバージョンが変わる

% pyenv global 3.7.16
% python --version
Python 3.7.16

今いるディレクトリのみバージョンを変更する

.python-version ファイルにバージョンが書き込まれることでそのディレクトリ配下のみバージョンを変更できる

% pyenv local 3.8.16
% python --version
Python 3.8.16

pyenv local --unset」で、設定したディレクトリのバージョンを解除することもできる
(「.python-version」ファイルがある階層でしか解除できない)

コメント

タイトルとURLをコピーしました