Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Python GPIO

Mon Jan 21, 2013 3:59 pm

something very strange happened when I tried to import Python's GPIO library into a program. I got the follwowing erro message.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import RPi.GPIO as GPIO
SetupException: No access to /dev/mem. Try running as root
I just installed the library. I would Like to know what the problem is and how to solve it

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Python GPIO

Mon Jan 21, 2013 4:09 pm

The error message says it all... To access the GPIO with the GPIO library, you must run your application (or python) as root.

Try:

Code: Select all

sudo python
or

Code: Select all

sudo python yourscript.py
This is because you are accessing the system memory map directly via /dev/mem which is a protected resource. You must have root privileges to directly read/write to memory outside of your application data space.

Enjoy!
Bill

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Python GPIO

Mon Jan 21, 2013 6:15 pm

thanks it works now but I have absouloutly no idy how to run python from the command line. I would like it if someone would tell me the commands for saving, running programs and quiting python(Also does this work with Python 3?)

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Python GPIO

Tue Jan 22, 2013 5:49 pm

So how did you get it working then? The above instructions are clearly for the command-line... :roll:

This is a good read for anyone wanting to learn 'command-line' on about any system http://cli.learncodethehardway.org/book/
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Python GPIO

Wed Jan 23, 2013 1:01 am

Patrick222122 wrote:thanks it works now but I have absouloutly no idy how to run python from the command line. I would like it if someone would tell me the commands for saving, running programs and quiting python(Also does this work with Python 3?)
Either open a terminal window or dont start the x-window interface

yes with pyhton3 replace python with python3 e.g

Code: Select all

sudo python3
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “General programming discussion”