0

I have interfaced a push button switch and LEDs to GPIO.

Whenever I try to execute the script from command line, the code python /home/myPi/myProject/run.py gives me the error

RuntimeError: Unable to export GPIO. Try to run as root!

But if the same script works perfectly with sudo python /home/myPi/myProject/run.py

How to resolve this error? I want to run the script at startup.

Please guide me. Thanks

  • 1
    There is no error. Some things require root privileges and apparently Python's runtime support for GPIO is one of those things. When you use the sudo command, your user profile is treated as if it was "root". So you could use the sudo command in your startup script. Also please note that some startup things on Linux actually run as root, so your script running at startup MAY not even need root/sudo. Try it. Sorry I am not more of an expert or I would give you more specifics. – MartinMarty May 17 '17 at 6:00
2

Running Python as root is a VERY BAD IDEA but used to be necessary with some Python GPIO modules.

It should not be necessary anymore as long as you are using a reasonably up to date Raspbian system.

Make sure your user (assumed to be pi) is a member of the gpio group.

sudo adduser pi gpio

1

The error is trying to tell you what to do

Try to run as root!

To run a command as root, you put the word sudo at the start of the command you want to execute. Your command should look like

sudo python /home/myPi/myProject/run.py

Be careful using sudo, using it gives you full permissions and you could break your operating system with it.

0

I had a similar problem where it wouldn't run programs without root access. I solved it by running the file from the terminal. To do this first go to the directory your file is in then type this:

sudo python your-filename.py
0

gpio library need sudo privileges so you should run your program sudo python yourProgram.py.

Unless there is a specific reason I would suggest to use Python3: sudo python3 yourProgram.py.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.