0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ラズパイ4にPython3.11をインストールする

Posted at

概要

2025年11月現在、Raspberry Pi 4に、Raspberry Pi OS LiteのOSインストールからやってみたところ、GPIO周りのモジュールがインストールできませんでした。この解決方法が良いかどうかは調査が必要ですが、とりあえず以下の手順でPythonからGPIOを操作できるようになったので、個人的手順書として残しておきます。

環境

  • Raspberry Pi 4 Model B Rev 1.5
  • Raspberry Pi OS Lite (64-bit)
cat /etc/os-release
# PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
# NAME="Debian GNU/Linux"
# VERSION_ID="13"
# VERSION="13 (trixie)"
# VERSION_CODENAME=trixie
# DEBIAN_VERSION_FULL=13.2
# ID=debian
# HOME_URL="https://www.debian.org/"
# SUPPORT_URL="https://www.debian.org/support"
# BUG_REPORT_URL="https://bugs.debian.org/"
uname -a
# Linux raspberrypi 6.12.47+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.47-1+rpt1 (2025-09-16) aarch64 GNU/Linux

python --version
# Python 3.13.5

現状の確認

最新版のPython3.13がインストールされているようでした。Python3.13ではpygpioが使えないようでした。

sudo apt -y update
sudo apt-get -y install pigpio pigpiod
# Reading package lists... Done
# Building dependency tree... Done
# Reading state information... Done
# Package pigpio is not available, but is referred to by another package.
# This may mean that the package is missing, has been obsoleted, or
# is only available from another source
# 
# E: Package 'pigpio' has no installation candidate
# E: Unable to locate package pigpiod

手順

やっていることは、古いバージョンのPythonをインストールし、そのpythonからGPIO周りのツールを使うようにしています。

Python3.11の確認

Download Python | Python.orgから、Python3.11系の最新版を確認します。2025年11月30日現在、3.11の最新版は、3.11.14 (Oct. 9, 2025) のようです。

Pythonをソースからビルド

以下の手順でPython3.11をソースからビルドします。

# ビルドに必要な依存関係をインストール
sudo apt install build-essential zlib1g-dev  libgdbm-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev tk-dev -y

# Python3.11のソースコードを取得
cd /tmp

# 3.11の最新版は、3.11.14 (Oct. 9, 2025)
wget https://www.python.org/ftp/python/3.11.14/Python-3.11.14.tgz
tar -xf Python-3.11.14.tgz
cd Python-3.11.14/

./configure --enable-optimizations
# 処理完了に1分ほどかかる

make -j4
# かなり時間がかかる
# 私の環境では17分かかりました

# インストール
sudo make altinstall

python3.11 --version
# Python 3.11.14
python3.11 -m pip --version
# pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)

# pigpioをインストール
python3.11 -m pip install pigpio
python3.11 -m pip install RPi.GPIO
python3.11 -m pip install rpi-hardware-pwm

# 不要なファイルを削除
cd ../
pwd
# /tmp
sudo rm -rf Python-3.11.14 Python-3.11.14.tgz

動作確認

Pythonでの確認

vim test.py
test.py
""" TARGET_GPIOをオン、オフします(Lチカを想定)
"""
import time
import RPi.GPIO as GPIO  # RPi.GPIOモジュールをインポート

INTERVAL = 0.5
TARGET_GPIO = 26

GPIO.setmode(GPIO.BCM)  # BCM(GPIO番号)で指定する設定
GPIO.setup(TARGET_GPIO, GPIO.OUT)  # 出力モード設定

try:
    while True:
        print("on")
        GPIO.output(TARGET_GPIO, GPIO.HIGH)
        time.sleep(INTERVAL)

        print("off")
        GPIO.output(TARGET_GPIO, GPIO.LOW)
        time.sleep(INTERVAL)
except KeyboardInterrupt as ex:
    print(str(ex))
    GPIO.output(TARGET_GPIO, GPIO.LOW)  # 必ず消灯

GPIO.cleanup()

実行。

phthon3.11 test.py
# Ctrl + C で終了

gpioset コマンドで確認

GPIOを確認(コマンドで)。

# GPIO 16(PIN番号36)をON
gpioset -c gpiochip0 16=1
# Ctrl + C で終了

# GPIO 16(PIN番号36)をOFF
gpioset -c gpiochip0 16=0
# Ctrl + C で終了

以上です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?