[Debian][Ansible][自分用メモ] Debian 10 (buster)でpython3をpythonのデフォルトに設定する

AIX、UNIX、Linux

最初に

最近はPythonについて、Python 2については今後のアップデートがないため事情がない限りはPython 3を使用することが推奨されていると思います。

とはいえ、Debian 10 (Buster)についてはリリースの関係からpythonのデフォルトは2で設定されており、 python3.x系列を利用したい場合には明示的にpython3を実行する必要があります。

第581回 Debian 10 "Buster"の紹介 | gihyo.jp
去る2019年7月6日に、Ubuntuの母であるDebianの新しいリリースであるDebian 10(コードネーム"Buster")がリリースされました。今回のUbuntu Weekly Recipeでは、このDebian 10 "Bust...

しかしながらAnsibleをDebian 10なマシンに対して実行すると以下のようなメッセージが表示されます。

PLAY [all] *******************************************************************************************************************
 
TASK [apt install packages] **************************************************************************************************
 [WARNING]: Platform linux on host xxx.xxx.xxx.xx2 is using the discovered Python interpreter at /usr/bin/python, but future  
installation of another Python interpreter could change this. See 
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.                    
    
ok: [xxx.xxx.xxx.xx2]
 [WARNING]: Platform linux on host xxx.xxx.xxx.xx1 is using the discovered Python interpreter at /usr/bin/python, but future  
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.                    
 
ok: [xxx.xxx.xxx.xx1]   

というわけで実行するPythonのバージョンを指定する方法をメモしておこうと思います。

確認環境

$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
 
$ uname -mr
4.19.85-mvebu64 aarch64

予めDebianのデフォルトで使用するバージョンを変更する

update-alternatives --install /usr/bin/python python /usr/bin/python3 1
update-alternatives --install /usr/bin/python python /usr/bin/python2 2
update-alternatives --set python /usr/bin/python3

設定できていると以下のような感じになっているかと思います。

$ sudo update-alternatives --config python 
[sudo] password for kometch: 
There are 2 choices for the alternative python (providing /usr/bin/python).
 
  Selection    Path              Priority   Status
------------------------------------------------------------
  0            /usr/bin/python2   2         auto mode
  1            /usr/bin/python2   2         manual mode
* 2            /usr/bin/python3   1         manual mode
 
Press <enter> to keep the current choice[*], or type selection number: 
Just a moment...

全体のPythonのデフォルトバージョンを変更する場合は、他のプログラムの動作に支障が出る場合があります。私は今のところそういう事態に陥ったことはないのですが、プログラムを精査する必要はあると思います。

Ansibleでサクッと設定する場合

とりあえず自分は以下のようなplaybookを用意して設定するようにしています。

---
- hosts: all
  become: True
 
  tasks:
  - name: set python3 as default
    shell: |
      update-alternatives --install /usr/bin/python python /usr/bin/python3 1
      update-alternatives --install /usr/bin/python python /usr/bin/python2 2
      update-alternatives --set python /usr/bin/python3
    when:
      ansible_distribution == "Debian"

Ansible側で実行するPythonを指定する

Ansibleで明示的にPython3で実行するようにするにはansible.cfg[defaults]セクションにinterpreter_python=/usr/bin/python3を設定します。

[defaults]
ansible_python_interpreter=/usr/bin/python3

このことについては最初のlogにも記載がありますが、公式ドキュメントに専用のページが用意されています。

ちなみに

あとはpyenv(Virtual Environment)で設定する方法もあります。

参考

Python Default Version - Raspberry Valley
How To Install Python 3 and Set Up a Programming Environment on Debian 10 | DigitalOcean
Python is a flexible and versatile programming language suitable for many use cases, including scripting, automation, da...
この記事を書いた人

kometchtech

うつ病を患いながら、IT業界の末席にいるおっさんエンジニア。科学計算をしたことがないのに、HPC分野にお邪魔している。興味のある分野で学習したことをblogにまとめつつ、うつ病の経過症状のメモも置いておく日々。じつはRouterboard User Group JPの中の人でもある。 Amazon欲しいものリスト / Arm板を恵んでくれる人募集中

kometchtechをフォローする
タイトルとURLをコピーしました