Many RaspberryPi users ask how to use GPIO without being root.
The short answer is that that's not possible.
The slightly longer answer is that GPIO really needs to be run as root. But that doesn't mean your whole python program needs to be run as root. This python module provides a command line interface (CLI) to GPI, so you can call it from shell, like so:
# Set pin 13 to HIGH, get its value, then set to LOW
sudo python gpiocli.py set 13 HIGH
sudo python gpiocli.py get 13
sudo python gpiocli.py set 13 LOW -q -v
sudo python gpiocli.py cleanup
import subprocess
subprocess.check_call(["sudo", "python", "gpiocli.py", "set", "13", "HIGH"])
subprocess.check_call(["sudo", "python", "gpiocli.py", "get", "13"])
# alternatively, depending on your shell environment
subprocess.check_call(["sudo python gpiocli.py get 13"], shell=True)
It's tempting to use setuid to make the module executable and run as root. However, that doesn't work!
Since the module starts with #!/usr/bin/python
, you can chmod +x
this script, and then run
sudo ./gpiocli.py 13 HIGH