Raspberry Pi 5 (bookworm 64bit) におけるPython環境の構築
はじめに
Ubuntuではpyenv環境で慣れているので、Raspberry Pi OSにおいてもpyenvでPython環境を整えたい。
そもそもpyenvとは、Pythonのバージョンを管理するツールで複数のバージョンのPythonを使い分けることを可能にしてくれます。たとえば、Globalにあるバージョンを指定し、あるフォルダ(開発プロジェクト毎など)をlocalとしてglobalと異なるバージョンのPythonを指定することで、開発プロジェクト毎に異なるバージョンの環境を提供可能です。
Pyenvのインストール
インストールはこれだけ、
1 2 3 4 5 6 7 8 9 |
19:42 rpi@raspberrypi5b-alpha:~ $git clone https://github.com/pyenv/pyenv.git ~/.pyenv Cloning into '/home/rpi/.pyenv'... remote: Enumerating objects: 23766, done. remote: Counting objects: 100% (2277/2277), done. remote: Compressing objects: 100% (337/337), done. remote: Total 23766 (delta 2013), reused 2102 (delta 1907), pack-reused 21489 Receiving objects: 100% (23766/23766), 4.75 MiB | 1.92 MiB/s, done. Resolving deltas: 100% (16127/16127), done. |
環境変数を設定します。
1 2 3 |
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init --path)"' >> ~/.bashrc |
環境変数を反映させ、インストールされたpyenvのバージョンを確認、インストールできるPythonのバージョンを確認。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
19:54 rpi@raspberrypi5b-alpha:~ $source .bashrc 19:54 rpi@raspberrypi5b-alpha:~ $pyenv --version pyenv 2.3.35-7-g6e3b91a8 19:54 rpi@raspberrypi5b-alpha:~ $pyenv install -list Available versions: 2.1.3 2.2.3 2.3.7 2.4.0 2.4.1 |
Pythonのインストール、失敗
足りないもの多数、ビルドできず
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
19:54 rpi@raspberrypi5b-alpha:~ $pyenv install 3.12.1 perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LC_ADDRESS = "ja_JP.UTF-8", LC_NAME = "ja_JP.UTF-8", LC_MONETARY = "ja_JP.UTF-8", LC_PAPER = "ja_JP.UTF-8", LC_IDENTIFICATION = "ja_JP.UTF-8", LC_TELEPHONE = "ja_JP.UTF-8", LC_MEASUREMENT = "ja_JP.UTF-8", LC_TIME = "ja_JP.UTF-8", LC_NUMERIC = "ja_JP.UTF-8", LANG = "en_GB.UTF-8" are supported and installed on your system. perl: warning: Falling back to a fallback locale ("en_GB.UTF-8"). Downloading Python-3.12.1.tar.xz... -> https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tar.xz Installing Python-3.12.1... Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/rpi/.pyenv/versions/3.12.1/lib/python3.12/bz2.py", line 17, in <module> from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2' WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/rpi/.pyenv/versions/3.12.1/lib/python3.12/curses/__init__.py", line 13, in <module> from _curses import * ModuleNotFoundError: No module named '_curses' WARNING: The Python curses extension was not compiled. Missing the ncurses lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/rpi/.pyenv/versions/3.12.1/lib/python3.12/ctypes/__init__.py", line 8, in <module> from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes' WARNING: The Python ctypes extension was not compiled. Missing the libffi lib? Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'readline' WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/rpi/.pyenv/versions/3.12.1/lib/python3.12/ssl.py", line 100, in <module> import _ssl # if we can't import it, let the error propagate ^^^^^^^^^^^ ModuleNotFoundError: No module named '_ssl' ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? BUILD FAILED (Debian 12 using python-build 2.3.35-7-g6e3b91a8) Inspect or clean up the working tree at /tmp/python-build.20240110195522.6283 Results logged to /tmp/python-build.20240110195522.6283.log Last 10 log lines: esac; \ LD_LIBRARY_PATH=/tmp/python-build.20240110195522.6283/Python-3.12.1 ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi Looking in links: /tmp/tmp8erb0wpc Processing /tmp/tmp8erb0wpc/pip-23.2.1-py3-none-any.whl Installing collected packages: pip WARNING: The scripts pip3 and pip3.12 are installed in '/home/rpi/.pyenv/versions/3.12.1/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-23.2.1 |
ライブラリのインストール
足りないライブラリや使うであろうtk-devをインストール。opencv関連は後でインストール。
1 2 3 |
sudo apt-get install make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev |
再度Pythonをインストール
再度Python3.12.1をインストール。できた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
20:22 rpi@raspberrypi5b-alpha:~ $pyenv install 3.12.1 perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LC_ADDRESS = "ja_JP.UTF-8", LC_NAME = "ja_JP.UTF-8", LC_MONETARY = "ja_JP.UTF-8", LC_PAPER = "ja_JP.UTF-8", LC_IDENTIFICATION = "ja_JP.UTF-8", LC_TELEPHONE = "ja_JP.UTF-8", LC_MEASUREMENT = "ja_JP.UTF-8", LC_TIME = "ja_JP.UTF-8", LC_NUMERIC = "ja_JP.UTF-8", LANG = "en_GB.UTF-8" are supported and installed on your system. perl: warning: Falling back to a fallback locale ("en_GB.UTF-8"). Downloading Python-3.12.1.tar.xz... -> https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tar.xz Installing Python-3.12.1... Installed Python-3.12.1 to /home/rpi/.pyenv/versions/3.12.1 20:25 rpi@raspberrypi5b-alpha:~ $pyenv versions * system (set by /home/rpi/.pyenv/version) 3.12.1 |
正常にインストールされたか確認、設定
localを設定して、versionsで確認。whichでも確認してみた。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
20:29 rpi@raspberrypi5b-alpha:~ $pyenv local 3.12.1 20:30 rpi@raspberrypi5b-alpha:~ $pyenv local 3.12.1 20:30 rpi@raspberrypi5b-alpha:~ $pyenv versions system * 3.12.1 (set by /home/rpi/.python-version) 20:30 rpi@raspberrypi5b-alpha:~ $which python /home/rpi/.pyenv/shims/python 20:35 rpi@raspberrypi5b-alpha:~ |
モジュールのインストール
前回出来なかった。pymysqlをインストール。ついでにpipの新しいバージョンがあると[お知らせ]がついていたので、pipもアップグレードした。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
20:35 rpi@raspberrypi5b-alpha:~ $python -m pip install pymysql Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting pymysql Downloading https://www.piwheels.org/simple/pymysql/PyMySQL-1.1.0-py3-none-any.whl (44 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.8/44.8 kB 130.9 kB/s eta 0:00:00 Installing collected packages: pymysql Successfully installed pymysql-1.1.0 [notice] A new release of pip is available: 23.2.1 -> 23.3.2 [notice] To update, run: pip install --upgrade pip 20:37 rpi@raspberrypi5b-alpha:~ $python -m pip install --upgrade pip Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: pip in ./.pyenv/versions/3.12.1/lib/python3.12/site-packages (23.2.1) Collecting pip Downloading https://www.piwheels.org/simple/pip/pip-23.3.2-py3-none-any.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 1.1 MB/s eta 0:00:00 Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 23.2.1 Uninstalling pip-23.2.1: Successfully uninstalled pip-23.2.1 Successfully installed pip-23.3.2 |