Python3系に置き換える

CentOSにインストールされるPythonは2.x系とちょっと古い。2.x系を使っているプログラムのために2.xを入れているようですが、自分は2.x系で書いたPythonプログラムを持っているわけでもなく、最新は3.xなので今後Ansibleとかの記事を書くために支障が出るので、今さらですが3.x系に置き換える

 

まずはひつようなリポジトリをインストール

    yum install -y https://centos7.iuscommunity.org/ius-release.rpm

次に3.x系のPythonを探しインストール。pythonの公式だと3.6が最新のようなので3.6で検索。

    

    yum search python36

    yum install python36u python36u-libs python36u-devel python36u-pip

インストールしたらまずは確認

    python

結果は以下の通り。まだ2.xがデフォルトなのでバージョンが2.7.5になっている。

    Python 2.7.5 (default, Aug  4 2017, 00:39:18)

    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2

    Type "help", "copyright", "credits" or "license" for more information.

    >>>

3.xが動くか確認するには以下のようにpythonの後ろにバージョン番号を明示的に指定する必要がある。

    python3.6

結果は以下の通り。

    Python 3.6.4 (default, Dec 19 2017, 14:48:12)

    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux

    Type "help", "copyright", "credits" or "license" for more information.

    >>>

これでも一応動くのですが、通常使うPythonのバージョンを3.6にしたいので、2.7から3.6に切り替えます。

    ln -s /bin/python3.6 /bin/python3

    unlink /bin/python

    ln -s /bin/python3 /bin/python

    ln -s /bin/pip3.6 /bin/pip

もう一度pyhon

    # python

    Python 3.6.4 (default, Dec 19 2017, 14:48:12)

    [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux

    Type "help", "copyright", "credits" or "license" for more information.

    >>>

これで3.6に変わったので今後pythonの記事は3.x系で記載していきます。 ちなみに2.7に戻したい時は先ほどのリンカーを2.7で設定すればよいです。