🛠 Premier script Python sur la bête de 256 Mo
Objectives
- Create, edit, and run a first Python 3 script on Raspberry Pi OS legacy.
- Query the system (memory, temperature, frequency) from Python.
- Make a script executable and launch it at session startup.
Hardware
- Raspberry Pi Model A (ARM11 700 MHz, 256 MB of RAM shared between CPU/GPU).
- Full-size SD card with the legacy image, HDMI screen or serial console.
- USB keyboard (directly or via a small hub: only one USB port on the Model A).
Complete code
#!/usr/bin/env python3
"""Inventaire express du Model A."""
import subprocess, shutil, platform
def cmd(c):
return subprocess.run(c, shell=True, capture_output=True,
text=True).stdout.strip
print("machine :", platform.machine)
print("cpu :", cmd("awk -F: 'NR==1{print $2}' /proc/cpuinfo"))
print("temp :", cmd("vcgencmd measure_temp"))
print("freq :", cmd("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"), "kHz")
m = shutil.disk_usage("/")
print("sd libre:", m.free // (1024 * 1024), "Mo")
chmod +x inventaire.py && ./inventaire.py
Expected verification
The script displays armv6l, a temperature in degrees Celsius, the current frequency in kHz (700000 at maximum unthrottled), and the free space on the SD card. >
Common errors
- Forgetting the shebang or
chmod +x: "command not found" instead of execution. - Launching a browser or a heavy IDE: the 256 MB saturates quickly, prefer nano and headless mode.
vcgencmdnot found: the script is running on a non-Raspberry Pi OS distribution.- Mixed tabs/spaces in Python indentation:
IndentationError.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython