🛠 Premier script Python : rapport d'information système
Objectives
Write, make executable, and run a real Python 3 script: arguments, function, calling vcgencmd to read the SoC temperature and the current DVFS frequency. This is the foundation for all following tutorials.
Hardware
- Raspberry Pi 4B with Raspberry Pi OS (Desktop or Lite)
- A terminal (SSH or local)
- No electronics required
Complete code
#!/usr/bin/env python3
"""info.py - petit rapport systeme du Pi 4B."""
import argparse
import subprocess
def vcgencmd(arg):
r = subprocess.run(["vcgencmd", arg], capture_output=True, text=True)
return r.stdout.strip or "indisponible"
def main:
p = argparse.ArgumentParser(description="Rapport systeme Pi 4B")
p.add_argument("--temp", action="store_true", help="temperature du SoC")
a = p.parse_args
print("Firmware :", vcgencmd("version").splitlines[0])
if a.temp:
print("Temperature :", vcgencmd("measure_temp"))
print("Etranglement :", vcgencmd("get_throttled"))
print("Memoire :", vcgencmd("get_config arm_freq"))
if __name__ == "__main__":
main
Expected verification
chmod +x info.py then ./info.py --temp : one line Firmware, Temperature : temp=42.0'C approximately, get_throttled 0x0 (no throttling) and arm_freq=1500 by default on BCM2711.
Common errors
- Forgetting the shebang or chmod +x : ./info.py returns Permission denied.
- Running with python (Python 2 is absent from Bookworm) instead of python3.
- vcgencmd not found : you are on another distribution or a VM, not on Raspberry Pi OS.
- InconsistentIndentationError : mixing spaces and tabs.
- The argument separator : --temp must precede the execution, not the editor.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython