CentOS7にpyenv

そろそろ真面目にPythonに取り掛かろうと、環境準備の一貫で異なるバージョンのPython環境を揃えることができるpyenvをインストールする。

公式サイト(GITHUB)

依存関係準備

$ sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite 
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils

インストール

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

pyenvのPATH設定

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
# Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; thenn eval "$(pyenv init -)"nfi' >> ~/.bash_profile
$ source ./.bash_profile

完了。

確認

現状チェック。

$ python -V
Python 2.7.5

pyenv で3.7.0をインストール。

$ pyenv install 3.7.0
Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
Installing Python-3.7.0...
Installed Python-3.7.0 to /home/centos/.pyenv/versions/3.7.0
$ pyenv versions
* system (set by /home/centos/.pyenv/version)
3.7.0

script directoryだけをversion 3.7.0 にする。

$ pwd
/home/centos/script
$ pyenv local 3.7.0
$ pyenv versions
system
* 3.7.0 (set by /home/centos/script/.python-version)
$ cd
$ pyenv versions
* system (set by /home/centos/.pyenv/version)
3.7.0

へー面白い。