LoginSignup
4
1

More than 1 year has passed since last update.

Python の Default Version 切り替えてみてみた

Last updated at Posted at 2023-03-05

Oracle Linux 8 や Red Hat Enterprise Linux 8 では、Python 3 は、AppStream リポジトリーの python36、python38、および python39 モジュールにより提供されるバージョン 3.6、3.8、および 3.9 で配布されます。 バージョンを指定しない python コマンドを使用して Python をインストールまたは実行すると、曖昧なためデフォルトでは動作しません。Python のバージョンを常に指定するか、alternatives コマンドを使用してシステムのデフォルトバージョンを設定します。
ということで、デフォルトの Python Version を変更設定してみてみます。

■ 現在の Python確認

1) Default Python Version確認

[root@linux8-inst ~]# python -V
	Python 3.6.8

2)Python モジュール一覧確認

dnf module listコマンドでインストールされている Pythonを確認
有効なモジュールは文字[e]で示されます。

Dandified YUM (DNF)パッケージマネージャー は、Red Hat8系のLinuxディストリビューションで使われている“RPMパッケージ”を扱うためのパッケージ管理コマンドです。ソフトウェアのインストール(install)や更新(update)、アンインストール(remove)の際に利用します。

[root@linux8-inst ~]# dnf module list | grep python
	libselinux-python    2.8             common                                   Python 2 bindings for libselinux
	python27             2.7 [d][e]      common [d]                               Python programming language, version 2.7
	python36             3.6 [d][e]      build, common [d]                        Python programming language, version 3.6
	python38             3.8 [d]         build, common [d]                        Python programming language, version 3.8
	python39             3.9 [d]         build, common [d]                        Python programming language, version 3.9

■ Python3.9 インストールと有効化

Python3.9 インストールされてない場合インストールします
有効になってない場合は、有効化します

● Python3.9 インストール

dnf module コマンドでインストールします

[root@linux8-inst ~]# dnf install python39
	This system is receiving updates from OSMS server.
	Last metadata expiration check: 0:02:58 ago on Thu 09 Mar 2023 02:48:53 AM GMT.
	Dependencies resolved.
	=================================================================================================================================================================================
	 Package                                      Architecture              Version                                                    Repository                               Size
	=================================================================================================================================================================================
	Installing:
	 python39                                     x86_64                    3.9.13-2.module+el8.7.0+20879+a85b87b0                     ol8_appstream-x86_64                     33 k
	Installing dependencies:
	 python39-libs                                x86_64                    3.9.13-2.module+el8.7.0+20879+a85b87b0                     ol8_appstream-x86_64                    8.2 M
	 python39-pip-wheel                           noarch                    20.2.4-7.module+el8.6.0+20625+ee813db2                     ol8_appstream-x86_64                    1.1 M
	 python39-setuptools-wheel                    noarch                    50.3.2-4.module+el8.5.0+20364+c7fe1181                     ol8_appstream-x86_64                    497 k
	Installing weak dependencies:
	 python39-pip                                 noarch                    20.2.4-7.module+el8.6.0+20625+ee813db2                     ol8_appstream-x86_64                    1.9 M
	 python39-setuptools                          noarch                    50.3.2-4.module+el8.5.0+20364+c7fe1181                     ol8_appstream-x86_64                    871 k
	
	Transaction Summary
	=================================================================================================================================================================================
	Install  6 Packages
	
	Total download size: 13 M
	Installed size: 45 M
	Is this ok [y/N]: y
	Downloading Packages:
	(1/6): python39-pip-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch.rpm                                                                             13 MB/s | 1.9 MB     00:00
	(2/6): python39-setuptools-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch.rpm                                                                     5.4 MB/s | 871 kB     00:00
	(3/6): python39-setuptools-wheel-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch.rpm                                                               2.7 MB/s | 497 kB     00:00
	(4/6): python39-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64.rpm                                                                                474 kB/s |  33 kB     00:00
	(5/6): python39-pip-wheel-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch.rpm                                                                      7.1 MB/s | 1.1 MB     00:00
	(6/6): python39-libs-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64.rpm                                                                            29 MB/s | 8.2 MB     00:00
	---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	Total                                                                                                                                             27 MB/s |  13 MB     00:00
	Running transaction check
	Transaction check succeeded.
	Running transaction test
	Transaction test succeeded.
	Running transaction
	  Preparing        :                                                                                                                                                         1/1
	  Installing       : python39-pip-wheel-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                                                                                        1/6
	  Installing       : python39-setuptools-wheel-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                                                                                 2/6
	  Installing       : python39-libs-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                                                                                             3/6
	  Installing       : python39-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                                                                                                  4/6
	  Running scriptlet: python39-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                                                                                                  4/6
	  Installing       : python39-setuptools-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                                                                                       5/6
	  Running scriptlet: python39-setuptools-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                                                                                       5/6
	  Installing       : python39-pip-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                                                                                              6/6
	  Running scriptlet: python39-pip-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                                                                                              6/6
	  Verifying        : python39-setuptools-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                                                                                       1/6
	  Verifying        : python39-setuptools-wheel-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                                                                                 2/6
	  Verifying        : python39-pip-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                                                                                              3/6
	  Verifying        : python39-pip-wheel-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                                                                                        4/6
	  Verifying        : python39-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                                                                                                  5/6
	  Verifying        : python39-libs-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                                                                                             6/6
	
	Installed:
	  python39-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64                               python39-libs-3.9.13-2.module+el8.7.0+20879+a85b87b0.x86_64
	  python39-pip-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch                           python39-pip-wheel-20.2.4-7.module+el8.6.0+20625+ee813db2.noarch
	  python39-setuptools-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch                    python39-setuptools-wheel-50.3.2-4.module+el8.5.0+20364+c7fe1181.noarch
	
	Complete!

● Python3.9 有効化

インストールしたパッケージがシステムで使用できるようにモジュールを有効(Enable)にします

[root@linux8-inst ~]# dnf module enable python39
	Last metadata expiration check: 1:28:39 ago on Sun 05 Mar 2023 10:13:16 AM GMT.
		Dependencies resolved.
		=================================================================================================================================================================================
		 Package                                   Architecture                             Version                                      Repository                                 Size
		=================================================================================================================================================================================
		Enabling module streams:
		 python39                                                                           3.9
		
		Transaction Summary
		=================================================================================================================================================================================
		
		Is this ok [y/N]: y
		Complete!

● Python3.9 インストールと有効化確認

1)Python モジュール一覧確認

[e]がついてEnableになったことを確認

[root@linux8-inst ~]# dnf module list | grep python
	libselinux-python    2.8             common                                   Python 2 bindings for libselinux
	python27             2.7 [d][e]      common [d]                               Python programming language, version 2.7
	python36             3.6 [d][e]      build, common [d]                        Python programming language, version 3.6
	python38             3.8 [d]         build, common [d]                        Python programming language, version 3.8
	python39             3.9 [d][e]      build, common [d]                        Python programming language, version 3.9

■ Python Version 変更

今回、デフォルトの Python Version を python3.6 から python3.9 へ変更してみてみます。

1) 現在設定されている Version確認

[root@linux8-inst ~]# python -V
	Python 3.6.8

[root@linux8-inst ~]# alternatives --list | grep python
	python                	manual	/usr/bin/python3
	python3               	auto  	/usr/bin/python3.6

2) Python List確認

現在使用可能な Pythonモジュールを表示するには、dnf module listを使用します。

[root@linux8-inst ~]# dnf module list | grep python
	libselinux-python    2.8             common                                   Python 2 bindings for libselinux
	python27             2.7 [d][e]      common [d]                               Python programming language, version 2.7
	python36             3.6 [d][e]      build, common [d]                        Python programming language, version 3.6
	python38             3.8 [d]         build, common [d]                        Python programming language, version 3.8
	python39             3.9 [d][e]      build, common [d]                        Python programming language, version 3.9

3) 変更したい Python Version のファイル・パス確認

[root@linux8-inst ~]# alternatives --display python3
	python3 - status is auto.
	 link currently points to /usr/bin/python3.6
	/usr/bin/python3.6 - priority 1000000
	 slave easy_install-3: /usr/bin/easy_install-3.6
	 slave pip-3: /usr/bin/pip-3.6
	 slave pip3: /usr/bin/pip3.6
	 slave pydoc-3: /usr/bin/pydoc3.6
	 slave pydoc3: /usr/bin/pydoc3.6
	 slave pyvenv-3: /usr/bin/pyvenv-3.6
	 slave python3-man: /usr/share/man/man1/python3.6.1.gz
	/usr/bin/python3.9 - priority 3900
	 slave easy_install-3: /usr/bin/easy_install-3.9
	 slave pip-3: /usr/bin/pip-3.9
	 slave pip3: /usr/bin/pip3.9
	 slave pydoc-3: /usr/bin/pydoc3.9
	 slave pydoc3: /usr/bin/pydoc3.9
	 slave pyvenv-3: (null)
	 slave python3-man: /usr/share/man/man1/python3.9.1.gz
	Current `best' version is /usr/bin/python3.6.

4) Python Version 変更設定

alternatives コマンドで設定

[root@linux8-inst ~]# alternatives --set python3 /usr/bin/python3.9

5) Version 変更確認

[root@linux8-inst ~]# alternatives --list | grep python
	python                	manual	/usr/bin/python3
	python3               	manual	/usr/bin/python3.9

[root@linux8-inst ~]# python -V
	Python 3.9.13

■ Aliasコマンドを使用した一時的 Python Version 変更

ログインしているセッションで一時的にPython バージョン変更したい場合は aliasコマンドで設定します。

● Alias設定

1) 変更前バージョン確認

[oracle@linux8-inst ~]# python -V
	Python 3.6.8

2) Alias設定

[oracle@linux8-inst ~]# alias python='/usr/bin/python3.9'

3) 変更後バージョン確認

[oracle@linux8-inst ~]# python -V
	Python 3.9.13

● Alias削除

aliasで設定したコマンドは、再ログインもしくは unaliasコマンドでリセットできます。

1) Alias確認

[oracle@linux8-inst ~]# alias | grep python
	alias python='/usr/bin/python3.9'

2) Alias削除

[oracle@linux8-inst ~]# unalias python

3) Alias削除確認

[oracle@linux8-inst ~]# alias | grep python
[oracle@linux8-inst ~]# python -V
    Python 3.6.8

■ 参考

 ・ Oracle Linux: Pythonのインストールと管理
 ・ Oracle Linux: DNF使用
 ・ Red Hat: Python のインストールおよび使用
 ・ Red Hat: P利用可能なモジュールとその内容の一覧表示

4
1
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
4
1