Is it possible to create/save a python script in the root folder, if so, how? The reason I ask this is because I am trying to use a single GPIO LED with python but when I run the script it gives an error message that it "has no access to /dev/mem. try running as root"

Here is the full error message.

Traceback (most recent call last) : ///new line/// File "/home/pi/LED.py, line 3, in ///new line/// GPIO.setup(7, GPIO.OUT) ///new line/// RuntimeError: No access to /dev/mem. Try running as root!

Just to make sure I was clear, the "///new line///" parts in the message were simply to signify where the lines should be, as I have not found how to insert line breaks on this website.

I am using Raspbian wheezy.(If anyone knows whether the default user for this operating system is a superuser, please leave a comment)

Thank you


1 Answer 1


The default Pi user can gain root level (superuser) access via the sudo command, or to avoid having to reenter the password become the root user by doing sudo su and supplying the password for the user Pi (this method is not recommend as a typo can hose your system, and has negative security implications).

The error message you receive when running the file

"Try running as root!"

is the result of the GPIO library needing root access to control the GPIO pins. to run your script either:

prepend the filename with sudo e.g. sudo mypythonscript.py, or sudo python filename e.g sudo python mypythonscript.py.

You will want to invest a little time here to learn about sudo because it is a common need (not just when accessing hardware, but for updating your system etc.). You can start by either doing a google search or by running the following command man sudo and man sudoers. These commands will load the manual pages (help file) for the sudo command and the file which controls it sudoers. Most Linux commands have at least one man page, and should often be the first resource you consult. I have been using Linux/Unix since the mid 90's and still refer to the man pages regularly, to check an option for a command that I don't use regularly - especially since I often run from the command line and don't have browser access.

  • 1
    It may be worth noting that it's no longer necessary to run as root to access the GPIO on the more recent (jessie) Raspbian images. The GPIO may now be accessed via /dev/gpiomem rather than /dev/mem. Members of the gpio group have access to /dev/gpiomem. The pi user defaults to being a member of group gpio.
    – joan
    Commented Nov 28, 2015 at 11:25
  • @joan, You took the words right out of my mouth. I was editing my answer to add additional details like GPIO access permissions, you may want to also mention that IIRC your library does not require root level permission either. Commented Nov 28, 2015 at 11:31
  • @SteveRobillard Thank you, I used the "sudo python mypythonscript.py" idea and It is now working. :) Commented Nov 29, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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