目的
この記事では、Debian 9StretchLinuxでPython2とPython3を切り替える方法について説明します
オペレーティングシステムとソフトウェアのバージョン
- オペレーティングシステム: –Debian9ストレッチ
要件
DebianLinuxインストールへの特権アクセスが必要になります。
難易度
簡単
規約
- # –指定されたLinuxコマンドは、rootユーザーとして直接、または
sudo
を使用して、root権限で実行する必要があります。 コマンド - $ –特定のLinuxコマンドを通常の非特権ユーザーとして実行する必要があります
手順
Debian9Stretchには2つの2.7
が付属しています および3.5
Pythonバージョン。まだPythonパッケージをインストールしていない場合は、適切なパッケージをインストールするだけで両方を選択できます。
PYTHON 2 INSTALLATION: # apt install python PYTHON 3 INSTALLATION: # apt install python3
デフォルトのPythonバージョンを確認する
デフォルトのPythonバージョンを確認するには、python
を実行するだけです。 コマンドを実行してそのバージョンを照会します:
$ python --version Python 2.7.13
Pythonのインストール
Pythonインタープリターがまだシステムにインストールされていないと仮定しましょう。したがって、両方のPythonバージョンをインストールすることから始めることができます:
# python --version -bash: python: command not found # apt install python python3
インストール後、Pythonバージョン2.7
デフォルトになります:
$ python --version Python 2.7.13
Python代替リストの更新
デフォルトのPythonバージョン間でシステム全体の切り替えを実行するには、update-alternatives
を使用します 指図。最初にupdate-alternatives
コマンドは、利用可能なPythonの代替手段がないことを訴えます:
# update-alternatives --list python update-alternatives: error: no alternatives for python
Pythonの代替をインストールするには、最初に利用可能なすべてのオプションをリストします。
$ ls /usr/bin/python* /usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python3m
次に、使用したいバージョンごとにPythonの代替リストを更新します。この場合、/usr/bin/python2.7
のスティックを使用します および/usr/bin/python3.5
バージョン:
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode # update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2 update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode
各コマンドの最後にある整数は優先順位を示していることに注意してください。数値が大きいほど優先度が高くなり、/usr/bin/python3.5
バージョンは、他の選択が選択されていない場合のデフォルトとして自動モードで設定されました。上記の両方のコマンドを実行した後、現在のデフォルトのpythonバージョンは/usr/bin/python3.5
です。 優先度が高いため(2
):
# python --version Python 3.5.3
Pythonバージョンの切り替え
これで、Pythonの代替のリストが更新され、Pythonのバージョンを切り替えることができます。
# update-alternatives --config python There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.5 2 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.5 2 manual mode Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode
上記のように、選択整数を使用して適切なバージョンを選択します。
# python --version Python 2.7.13
ローカルユーザーPythonバージョン
ユーザーごとにPythonのバージョンを選択的に変更するだけでよい場合は、ユーザーの.bashrc
を編集してみてください。 ファイル。たとえば、Pythonバージョン3.5
に変更するには 次のLinuxコマンドを実行します。
$ python --version Python 2.7.13 $ echo 'alias python="/usr/bin/python3.5"' >> ~/.bashrc $ . .bashrc $ python --version Python 3.5.3
{loadposition python-tutorial-toc}