projekt:python_erste_skripte
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| projekt:python_erste_skripte [2026/02/08 17:28] – torsten.roehl | projekt:python_erste_skripte [2026/02/08 18:45] (aktuell) – torsten.roehl | ||
|---|---|---|---|
| Zeile 4: | Zeile 4: | ||
| [[python| ☚ zurück]] | [[python| ☚ zurück]] | ||
| + | //In diesem Abschnitt geht es darum zu lernen, wie man Python-Skripte erstellt und startet. Dabei soll eine Python-Environment genutzt werden. Nach dem Durcharbeiten dieser Seite kann mit den eigentlichen Projekten begonnen werden.// | ||
| + | * Anlegen und Nutzen einer **env** | ||
| + | * Erstellen und Ausführen von Python-Skripten | ||
| + | * Einbinden und Gebrauch von Modulen | ||
| + | * Projektstruktur und Arbeiten mit mehreren Dateien | ||
| - | ====== Raspberry-Pi-Kursprojekt: | ||
| - | * erst reines Python | ||
| - | * dann Dauerprozess | ||
| - | * dann Web-API | ||
| - | * dann Apache davor | ||
| - | * EIN Projekt, keine Neuentwicklung | ||
| - | **ENV:** gpio_projects | + | ===== Example 0: Environment anlegen ===== |
| - | **Projektpfad: | + | |
| - | ===== PHASE 1 – Reines | + | <WRAP center round box 80%> |
| + | **Aufgabe: Anlegen einer Python-Environment** | ||
| + | Lege eine Umgebung | ||
| + | * '' | ||
| + | Hierfür muss der Ordner '' | ||
| + | </ | ||
| - | ==== 0) System vorbereiten ==== | + | <note tip> |
| + | Das Anlegen und Managen einer Umgebung ist hier | ||
| + | [[python_python-umgebung_env_einrichten_vorbereitungen_fuer_projekte_schaffen|Python ENV]] | ||
| + | ausführlich beschrieben. | ||
| + | </ | ||
| - | <code bash> | + | Wechsle anschließend in die Umgebung, um die Examples zu bearbeiten! |
| - | sudo apt update | + | |
| - | sudo apt install -y python3 python3-pip python3-venv | + | |
| - | </ | + | |
| - | ---- | + | ===== Example 1: " |
| - | ==== 1) Projektordner anlegen ==== | + | <code python> |
| + | # | ||
| - | <code bash> | + | # Testet die Reaktionszeit |
| - | mkdir -p ~/ | + | # @author: Max Mustermann |
| - | cd ~/ | + | # @version: 1.0 |
| - | </code> | + | # @usage: |
| - | ---- | ||
| - | ==== 2) Virtuelle Umgebung anlegen (NAME: gpio_projects) ==== | + | import time |
| + | import random | ||
| - | <code bash> | + | def main(): |
| - | python3 -m venv gpio_projects | + | print(" |
| - | source gpio_projects/ | + | print(" |
| + | | ||
| + | | ||
| - | pip install RPi.GPIO | + | wartezeit = random.uniform(2, 6) |
| - | </ | + | |
| - | ---- | + | print(" |
| - | + | ||
| - | ==== 3) Projektstruktur Phase 1 ==== | + | |
| - | + | ||
| - | < | + | |
| - | gpio_projects/ | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | + | ||
| - | (gpio_env) pi@raspi88: | + | |
| - | . | + | |
| - | ├── gpio_env | + | |
| - | ├── gpio_hw.py | + | |
| - | ├── logic.py | + | |
| - | └── main.py | + | |
| + | start = time.time() | ||
| + | input() | ||
| + | ende = time.time() | ||
| + | reaktion = ende - start | ||
| + | print(f" | ||
| + | if __name__ == " | ||
| + | main() | ||
| </ | </ | ||
| - | ---- | ||
| - | |||
| - | ==== 4) gpio_hw.py ==== | ||
| - | |||
| - | <code python | ||
| - | import RPi.GPIO as GPIO | ||
| - | |||
| - | PIN = 17 | ||
| - | |||
| - | GPIO.setmode(GPIO.BCM) | ||
| - | GPIO.setup(PIN, | ||
| - | |||
| - | def on(): | ||
| - | GPIO.output(PIN, | ||
| - | |||
| - | def off(): | ||
| - | GPIO.output(PIN, | ||
| - | |||
| - | def status(): | ||
| - | return GPIO.input(PIN) | ||
| - | |||
| - | def cleanup(): | ||
| - | GPIO.cleanup() | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ==== 5) logic.py ==== | ||
| - | |||
| - | <code python logic.py> | ||
| - | import gpio_hw | ||
| - | |||
| - | def turn_on(): | ||
| - | gpio_hw.on() | ||
| - | return " | ||
| - | |||
| - | def turn_off(): | ||
| - | gpio_hw.off() | ||
| - | return " | ||
| - | |||
| - | def get_status(): | ||
| - | return " | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ==== 6) main.py ==== | ||
| - | |||
| - | <code python | ||
| - | import logic | ||
| - | import gpio_hw | ||
| - | |||
| - | print(" | ||
| - | |||
| - | try: | ||
| - | while True: | ||
| - | cmd = input(" | ||
| - | if cmd == " | ||
| - | print(logic.turn_on()) | ||
| - | elif cmd == " | ||
| - | print(logic.turn_off()) | ||
| - | elif cmd == " | ||
| - | print(logic.get_status()) | ||
| - | except KeyboardInterrupt: | ||
| - | pass | ||
| - | finally: | ||
| - | gpio_hw.cleanup() | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ==== 7) Start Phase 1 ==== | ||
| - | |||
| - | <code bash> | ||
| - | cd ~/ | ||
| - | source gpio_projects/ | ||
| - | python3 main.py | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== PHASE 3 – Web später ===== | ||
| - | |||
| - | <code bash> | ||
| - | pip install fastapi uvicorn | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== systemd später mit dieser ENV ===== | ||
| - | |||
| - | < | ||
| - | ExecStart=/ | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== Ergebnis ===== | ||
| - | * ENV = gpio_projects | + | ===== Example 2: todo ===== |
| - | * Projekt | + | |
| - | * kein raspi_gpio_env mehr | + | |
| - | * konsistent für alle Phasen | + | |
projekt/python_erste_skripte.1770571720.txt.gz · Zuletzt geändert: von torsten.roehl
