Hi,
I know this is an old thread. I have exactly the same issue (Runtime error, failed to add edge detection), and tried a lot:
I've narrowed my issue down to:
Most simple version of script to recreate:When i run as a normal command in my user shell (either as pi user or root user), i have no issues, The script just logs everytime the button is pushed.
However when i run the script as a systemd service or in a docker container (which is the goal). I alway get the same issue:This is the docker container output, using this dockerfile:however running the same thing as a systemd service:also gives the same error.
Again: Running it from the command prompt runs fine (both as pi or root user)
I've tried forcing the pin factory (nor result) or installing the other lgpio libs, resulted in the same behaviour: Runs fine when running from the prompt, but fails under docker of systemd service
Is there anyone who can help me?
(BTW: I am running on a pi4 bookworm arm 64 bit, and just to rule out anything: Updated everything with apt update/upgrade)
EDIT: Found it: it was in a venv environment, when i update the correct libs and that venv environment it worked as a service (still need to fix for the docker, but step further now..)
I know this is an old thread. I have exactly the same issue (Runtime error, failed to add edge detection), and tried a lot:
I've narrowed my issue down to:
Most simple version of script to recreate:
Code:
#!/usr/bin/env python3from gpiozero import Buttonfrom signal import pause# Eenvoudige callbackdef on_press(): print("Knop ingedrukt (puls gedetecteerd)")# Maak Button op GPIO18 (BCM)button = Button(18)# Koppel callbackbutton.when_pressed = on_pressprint("Wacht op GPIO18 input (druk Ctrl+C om te stoppen)")pause()However when i run the script as a systemd service or in a docker container (which is the goal). I alway get the same issue:
Code:
buttontest-1 | /usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from lgpio: No module named 'lgpio'buttontest-1 | warnings.warn(buttontest-1 | Traceback (most recent call last):buttontest-1 | File "/app/buttontest.py", line 11, in <module>buttontest-1 | button = Button(18)buttontest-1 | ^^^^^^^^^^buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/devices.py", line 108, in __call__buttontest-1 | self = super().__call__(*args, **kwargs)buttontest-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/input_devices.py", line 412, in __init__buttontest-1 | super().__init__(buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/mixins.py", line 417, in __init__buttontest-1 | super().__init__(*args, **kwargs)buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/input_devices.py", line 168, in __init__buttontest-1 | self.pin.when_changed = self._pin_changedbuttontest-1 | ^^^^^^^^^^^^^^^^^^^^^buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/__init__.py", line 471, in <lambda>buttontest-1 | lambda self, value: self._set_when_changed(value),buttontest-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/pi.py", line 639, in _set_when_changedbuttontest-1 | self._enable_event_detect()buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/rpigpio.py", line 220, in _enable_event_detectbuttontest-1 | GPIO.add_event_detect(buttontest-1 | RuntimeError: Failed to add edge detectionusing this docker-compose.yaml:FROM python:3.11-slim
# Vereiste buildtools installeren
RUN apt-get update && apt-get install -y \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*
# Vereiste Python libs installeren
RUN pip install gpiozero RPi.GPIO
# App toevoegen
WORKDIR /app
COPY buttontest.py .
CMD ["python", "buttontest.py"]
Code:
services: buttontest: build: . privileged: true # noodzakelijk voor GPIO toegang devices: - "/dev/gpiomem:/dev/gpiomem" volumes: - /sys/class/gpio:/sys/class/gpio restart: unless-stoppedCode:
[Unit]Description=gpio2mqttAfter=network.target[Service]PermissionsStartOnly=falseExecStart=/home/pi/CarportGPIO2MQTT/bin/python /home/pi/CarportGPIO2MQTT/gpiomqtt.pyWorkingDirectory = /home/pi/CarportGPIO2MQTTType=simpleRestart=alwaysRestartSec=1[Install]WantedBy=multi-user.targetAgain: Running it from the command prompt runs fine (both as pi or root user)
I've tried forcing the pin factory (nor result) or installing the other lgpio libs, resulted in the same behaviour: Runs fine when running from the prompt, but fails under docker of systemd service
Is there anyone who can help me?
(BTW: I am running on a pi4 bookworm arm 64 bit, and just to rule out anything: Updated everything with apt update/upgrade)
EDIT: Found it: it was in a venv environment, when i update the correct libs and that venv environment it worked as a service (still need to fix for the docker, but step further now..)
Statistics: Posted by akamming — Sat Jul 05, 2025 8:53 am