2
1

More than 1 year has passed since last update.

BusterからBullseyeにアップデートしたRaspberry PiでPythonの動作環境を整える

Last updated at Posted at 2021-09-25

1. はじめに

Raspberry Pi 3 Model Bが死蔵状態にあり、これを活用するためにBullseyeにアップデートし、(後々Ansibleを動かしたいので)Pythonの動作環境を整えます。

2. バージョンの確認

$ python --version
-bash: python: command not found

BusterからBullseyeにするとpythonがない・・・?

$ python3 --version
Python 3.9.2

$ python2 --version
Python 2.7.18

3系、2系ともに残っていました。

3. 2系の削除

試しにsudo apt install pythonしてみます。

$ sudo apt install python
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 python : PreDepends: python-minimal (= 2.7.16-1) but it is not going to be installed
          Depends: python2.7 (>= 2.7.16-1~) but it is not going to be installed
          Depends: libpython-stdlib (= 2.7.16-1) but it is not going to be installed
          Depends: python2 (= 2.7.16-1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

問題があるようです。試しにsudo apt install python3してみます。

$ sudo apt install python3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3 is already the newest version (3.9.2-3).
python3 set to manually installed.
The following packages were automatically installed and are no longer required:
  libicu63 libmpdec2 libperl5.28 libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib libpython3.7 libpython3.7-minimal libpython3.7-stdlib perl-modules-5.28
  python-pkg-resources python2 python2-minimal python2.7 python2.7-minimal
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

python3はインストール済みのようです。sudo apt autoremoveを提案されました。やってみましょう。

$ sudo apt autoremove
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  libicu63 libmpdec2 libperl5.28 libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib libpython3.7 libpython3.7-minimal libpython3.7-stdlib perl-modules-5.28
  python-pkg-resources python2 python2-minimal python2.7 python2.7-minimal
0 upgraded, 0 newly installed, 15 to remove and 0 not upgraded.
After this operation, 112 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 38155 files and directories currently installed.)
Removing libicu63:arm64 (63.1-6+deb10u1) ...
Removing libpython3.7:arm64 (3.7.3-2+deb10u3) ...
Removing libpython3.7-stdlib:arm64 (3.7.3-2+deb10u3) ...
Removing libmpdec2:arm64 (2.4.2-2) ...
Removing libperl5.28:arm64 (5.28.1-6+deb10u1) ...
Removing python-pkg-resources (44.1.1-1) ...
Removing python2 (2.7.18-3) ...
Removing libpython2-stdlib:arm64 (2.7.18-3) ...
Removing python2.7 (2.7.18-8) ...
Removing libpython2.7-stdlib:arm64 (2.7.18-8) ...
Removing python2-minimal (2.7.18-3) ...
Removing python2.7-minimal (2.7.18-8) ...
Unlinking and removing bytecode for runtime python2.7
Removing libpython2.7-minimal:arm64 (2.7.18-8) ...
Removing libpython3.7-minimal:arm64 (3.7.3-2+deb10u3) ...
Removing perl-modules-5.28 (5.28.1-6+deb10u1) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for mailcap (3.69) ...
Processing triggers for libc-bin (2.31-13) ...

$ python2 --version
-bash: /usr/bin/python2: No such file or directory

$ sudo ls -al /usr/bin/py*
-rwxr-xr-x 1 root root    7806 Apr  5 21:00 /usr/bin/py3clean
-rwxr-xr-x 1 root root   13184 Apr  5 21:00 /usr/bin/py3compile
lrwxrwxrwx 1 root root      31 Apr  5 21:00 /usr/bin/py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root       8 Apr  5 21:00 /usr/bin/pydoc3 -> pydoc3.9
-rwxr-xr-x 1 root root      78 Mar  1  2021 /usr/bin/pydoc3.9
lrwxrwxrwx 1 root root      12 Apr  5 21:00 /usr/bin/pygettext3 -> pygettext3.9
-rwxr-xr-x 1 root root   21537 Feb 19  2021 /usr/bin/pygettext3.9
lrwxrwxrwx 1 root root       9 Apr  5 21:00 /usr/bin/python3 -> python3.9
-rwxr-xr-x 1 root root 5280744 Mar  1  2021 /usr/bin/python3.9

2系が削除されました。ところでpythonと打っていたコマンドは今後python3と打つのでしょうか?
何も考えずに作業は進めることにします。

4. pipのインストール

$ python3 -m pip --version
/usr/bin/python3: No module named pip

BusterからBullseyeにするとpipがない・・・?

If you’re on Linux and installed using your OS package manager, you may have to install pip separately, see Installing pip/setuptools/wheel with Linux Package Managers.
Linuxを使用していて、OSパッケージマネージャーを使用してインストールする場合は、pipを個別にインストールする必要がある場合があります。Linuxパッケージマネージャーを使用したpip / setuptools / wheelのインストールを参照してください。

Debian/Ubuntu向けの部分があります。
https://packaging.python.org/guides/installing-using-linux-tools/#debian-ubuntu

sudo apt update
sudo apt install python3-venv python3-pip
$ sudo apt install python3-venv python3-pip
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了        
状態情報を読み取っています... 完了        
以下の追加パッケージがインストールされます:
  javascript-common libexpat1-dev libjs-jquery libjs-sphinxdoc libjs-underscore libpython3-dev libpython3.9-dev python-pip-whl python3-dev python3-distutils python3-lib2to3
  python3-setuptools python3-wheel python3.9-dev python3.9-venv
提案パッケージ:
  apache2 | lighttpd | httpd python-setuptools-doc
以下のパッケージが新たにインストールされます:
  javascript-common libexpat1-dev libjs-jquery libjs-sphinxdoc libjs-underscore libpython3-dev libpython3.9-dev python-pip-whl python3-dev python3-distutils python3-lib2to3
  python3-pip python3-setuptools python3-venv python3-wheel python3.9-dev python3.9-venv
アップグレード: 0 個、新規インストール: 17 個、削除: 0 個、保留: 0 個。
7,975 kB のアーカイブを取得する必要があります。
この操作後に追加で 27.8 MB のディスク容量が消費されます。
続行しますか? [Y/n]Y

色々とインストールされました。

$ python3 -m pip --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)
$ pip --version
pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

pipが使えるようになりました。

Ensure pip, setuptools, and wheel are up to date
While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

pip、setuptools、およびwheelが最新であることを確認してください
ビルド済みのバイナリアーカイブからインストールするにはpipだけで十分ですが、setuptoolsとwheelプロジェクトの最新のコピーは、ソースアーカイブからもインストールできるようにするのに役立ちます。

とのことなので、アップデートを確認します。

$ python3 -m pip install --upgrade pip setuptools wheel
Requirement already satisfied: pip in /usr/lib/python3/dist-packages (20.3.4)
Collecting pip
  Downloading pip-21.2.4-py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 4.3 MB/s 
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (52.0.0)
Collecting setuptools
  Downloading setuptools-58.1.0-py3-none-any.whl (816 kB)
     |████████████████████████████████| 816 kB 5.1 MB/s 
Requirement already satisfied: wheel in /usr/lib/python3/dist-packages (0.34.2)
Collecting wheel
  Downloading wheel-0.37.0-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, setuptools, pip
  WARNING: The script wheel is installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts pip, pip3 and pip3.9 are installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-21.2.4 setuptools-58.1.0 wheel-0.37.0

アップデートされましたが、WARNING(警告)が出ています。

警告:スクリプトホイールは、PATH上にない '/home/pi/.local/bin'にインストールされています。
このディレクトリをPATHに追加することを検討してください。この警告を抑制したい場合は、-no-warn-script-locationを使用してください。

/home/pi/.local/binには何があるのでしょうか?

$ ls -al /home/pi/.local/bin
合計 24
drwxr-xr-x 2 pi pi 4096  9月 25 10:39 .
drwxr-xr-x 5 pi pi 4096  9月 25 10:39 ..
-rwxr-xr-x 1 pi pi  221  9月 25 10:39 pip
-rwxr-xr-x 1 pi pi  221  9月 25 10:39 pip3
-rwxr-xr-x 1 pi pi  221  9月 25 10:39 pip3.9
-rwxr-xr-x 1 pi pi  208  9月 25 10:39 wheel

pipwheelがありました。
では実行されているのはどのpipでしょうか?

$ python3 -m pip --version
pip 21.2.4 from /home/pi/.local/lib/python3.9/site-packages/pip (python 3.9)

$ pip --version
pip 21.2.4 from /home/pi/.local/lib/python3.9/site-packages/pip (python 3.9)

$ which pip
/usr/bin/pip

/home/pi/.local/lib/python3.9/site-packages/pipからのようです。
しかし、PATH上からコマンドを検索するwhichコマンドの結果は/usr/bin/pipを示しています。
では今のPATHはどうなっているのでしょうか?

$  echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

確かに/usr/bin/pipがありません。加えましょう。
Raspberry Pi OSにおいて、PATH.profileに記載されています。

$ cat .profile

中略

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

あるやんけ。

各ユーザーのホームディレクトリに.local/binがあればPATHに加える記述です。
pipのインストールにより.local/binが作成されたのでしょう。
.profileに記載されているので、再ログインしないと反映されません。

再ログインしてみます。

$ echo $PATH
/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

$ which pip
/home/pi/.local/bin/pip

解決。

今日はここまで。

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