Alternativesコマンドを詳細に説明します(この例では、例としてPythonバージョン切り替えを使用しています)

                                                  Alternativesコマンドを詳細に説明します(この例では、例としてPythonバージョン切り替えを使用しています)

Alternativesは、最小限のインストールでも、Linuxシリーズのオペレーティングシステムに組み込まれているコマンドです。その主な機能はバージョン管理の切り替えです。たとえば、システムにはPython3.8、Python2.7.5、などの複数のPythonバージョンがあります。およびPython3.6、。

まず、ソースコードを使用してコンパイルおよびインストールするときにインストールディレクトリを指定できるため、Pythonの複数のバージョンが同じシステムに共存できることを明確にする必要があります。rpmまたはyumでインストールされている場合は、バージョンが存在する可能性があります。競合の問題。さらに、Pythonの各バージョンには独自の特性があります。たとえば、Python3のメジャーバージョンには基本的にpipパッケージマネージャーが付属していますが、Python2.7にはpipがないため、手動でインストールする必要があります。区別するために、python3.8のpipバージョンは21.0.1であり、Python2.7.5のpipバージョンは20.3.4です。

環境紹介:

Centos7.2システムでは、組み込みのPython2.7.5とソースコードからインストールされたPython3.8.1を使用します。Python2.7.5は手動でpip20.3.4をインストールします。Pythonはソースコードからコンパイルされるため、pipが付属しています。 Python 3.8をインストールした後、pipを21.0.1にアップグレードします。

代替コマンドの概要:

[root@centos6 piprpm]# alternatives 
alternatives version 1.7.4 - Copyright (C) 2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage: alternatives --install <link> <name> <path> <priority>
                    [--initscript <service>]
                    [--family <family>]
                    [--slave <link> <name> <path>]*
       alternatives --remove <name> <path>
       alternatives --auto <name>
       alternatives --config <name>
       alternatives --display <name>
       alternatives --set <name> <path>
       alternatives --list

common options: --verbose --test --help --usage --version --keep-missing
                --altdir <directory> --admindir <directory>

一般的に使用される主なパラメーターは、インストール、削除、構成、表示、およびリストの5つのパラメーターです。

インストール-ソフト接続を生成します

削除-ソフト接続を削除します

config-ソフト接続を選択します

display-ソフト接続を表示します

リスト-すべてのソフト接続を表示



Python 3.8.1がインストールされていると仮定すると、インストールディレクトリは/usr/local/python3.8で指定され、Python2.7.5のpipはpip8.1.2でインストールされてから、すべてPythonのpip20.3.4にアップグレードされます。そしてpipは普通に使うことができます、

まず、Python3.8.1を代替管理に追加します。コマンドは次のとおりです。

Alternatives --install / usr / bin / python python /usr/local/python3.8/bin/python3.8 3このコマンドは、Python3.8を代替案に追加します。

次に、Python2.7.5を代替管理に追加します。コマンドは次のとおりです。

代替案--install / usr / bin / python python /usr/bin/python2.7 2

次に、PythonのバージョンをPython3.8に切り替えます

[root@centos6 ~]# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 2

2を選択した後、Python3.8.1バージョンに切り替えることができます 

/ usr / bin / python最初はどのファイルですか?

[root@centos7 ~]# ls -al /usr/bin/python
lrwxrwxrwx. 1 root root 7 Jan 23 22:13 /usr/bin/python -> python2

[root@centos7 bin]# ls -al /usr/bin/python2
lrwxrwxrwx. 1 root root 9 Jan 23 22:13 python2 -> python2.7

代替管理が追加される前は、/ usr / bin / python2を指すリンクファイルであり、Python2は/usr/bin/python2.7を指していることがわかります。

代替管理に参加した後、/ usr / bin / pythonの方向が再び異なります

[root@centos6 ~]# ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Mar 27 05:33 /usr/bin/python -> /etc/alternatives/python

[root@centos6 ~]# ls -al /etc/alternatives/python
lrwxrwxrwx 1 root root 34 Mar 27 05:33 /etc/alternatives/python -> /usr/local/python3.8/bin/python3.8

これは、Pythonバージョンを3.8に切り替えた後の/ etc / Alternatives / pythonのポイントです。/usr/local/python3.8/bin/python3.8にリンクされていることがわかります。

では、Python 2.7.5に切り替えた後はどうでしょうか?

[root@centos6 ~]# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 2^H

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 1
[root@centos6 ~]# ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Mar 27 06:50 /usr/bin/python -> /etc/alternatives/python
[root@centos6 ~]# ls -al /etc/alternatives/python
lrwxrwxrwx 1 root root 18 Mar 27 06:50 /etc/alternatives/python -> /usr/bin/python2.7

ここでは、alternatives --config pythonを使用した後、1を選択し、Python2.7に切り替えました。リンクを確認すると、/ etc / Alternatives / pythonが/usr/bin/python2.7lを指していることがわかります。

代替案--install / usr / bin / python python /usr/local/python3.8/bin/python3.8 3このコマンドはどのように記述されていますか?インストールが続くファイルはリンクファイルである必要があり、pythonはプロジェクト名、/ usr / local / python3.8 / bin / python3.8は実際のファイルの絶対パス、3は優先度です。2番目のコマンドで設定された優先度は2です。これは、自動モードの場合、優先度の高いPython3.8が最初に使用されることを示します。

下の図に示すように、2行目は手動モードを示しています。自動の場合は、Python3.8が最初に使用されます。

3行目は、現在/usr/bin/python2.7を使用していることを示し、4行目と5行目は、2つのバージョンの優先順位であり、6行目は、最適なバージョンがPython3.8であることを意味します。

[root@centos6 ~]# alternatives --display python
python - status is manual.
 link currently points to /usr/bin/python2.7
/usr/bin/python2.7 - priority 2
/usr/local/python3.8/bin/python3.8 - priority 3
Current `best' version is /usr/local/python3.8/bin/python3.8.

Pythonのバージョンが切り替えられると、pipが2つの異なるコンポーネントであっても、pipマネージャーが自動的に切り替えることに注意してください。 

次に、上記の実験の後、alternatives --config project nameを使用して、alternativesがリンクマネージャーに類似していると結論付け、シリアル番号を選択して使用するプロジェクトを選択できます。これにより、リンクポイントを動的に調整します。

同じシステム内に複数のgcc、tomcat、jdkバージョンがある場合は、バージョンの混乱を避けるために代替手段を使用できます。これは非常に便利なLinuxコマンドです。システムを再起動しても、手動モードである限り、Pythonのバージョン選択は維持されることに注意してください。)

おすすめ

転載: blog.csdn.net/alwaysbefine/article/details/115261860