A bit of help for the Python beginner

9 posts
by AforAlex » Sat Oct 06, 2012 11:16 pm
Hi, I'm just starting out using Python and I have a few questions if you don't mind answering. If they have been answered else where then any pointer on where to look would be much appreciated.

I'm using the standard latest Debian image, and using the LXDE. I'm trying to get my head around and have a play with using Python with Pygame and also access the GPIO

First question is what is the difference between IDLE and IDLE 3 and which one am I best to use?

Second question is how do I run my Python programme as root. I've run a couple of example Python programmes that access the GPIOs, and they work ok from terminal when run using the SUDO command but when I run them from within IDLE I am getting permission denied messages.

Any help and advice would be very much appriciated
Posts: 82
Joined: Thu May 31, 2012 7:56 pm
by AforAlex » Sat Oct 06, 2012 11:22 pm
Just to add, I am connecting to the RaspberryPi using TightVNC, I have tried running "sudo idle" and "sudo idle3" from a terminal window, but I get an error message saying "Client is not authorised to connect to ServerTraceback .........."
Posts: 82
Joined: Thu May 31, 2012 7:56 pm
by billb » Sun Oct 07, 2012 12:25 am
AforAlex wrote:First question is what is the difference between IDLE and IDLE 3 and which one am I best to use?

IDLE is for python 2.x and IDLE 3 is for python 3.x. Depending on which tutorials / examples you're following, you may want to use python 2 or 3. Google "python 2 vs 3" and you'll find lots of information about the differences. If you're following a book that is based on python 2 you'd definitely want to use IDLE / python 2.

As I understand it, Python 2 is still the default for Debian / Raspbian wheezy. So when you install pygame (from a terminal: sudo apt-get install python-pygame) the version installed is for python 2.

If you want to use pygame with IDLE 3 / python 3, you'll need to install it manually as described here:

(this probably takes around 30 minutes or so)

I haven't tried any of the GPIO stuff yet so can't help there. Looking forward to it, though. :)
User avatar
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm
by rickseiden » Sun Oct 07, 2012 6:41 pm
I've done a few things with GPIO, and I've always used IDLE, which is for Python 2, and not IDLE 3 which is for Python 3. While I would love to be able to run it from IDLE, I haven't been able to figure that one out myself. I've always just run them from a command line using sudo, as you've mentioned.

Not much help, but maybe it's a little.

There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by AforAlex » Mon Oct 08, 2012 12:35 pm
Thank you very much, very useful.

I've been having a play, i think i have opted for Geany as an IDE to use ... I don't know what it is, but just looks and feels a little bit cleaner than IDLE.

I've managed to also work out the other problem I was having which was to do with user rights and accessing the XServer remotely using TightVNC. Bassically the VNC was running under the user Pi, which was not root, I could run commands in Terminal with the SUDO command, but I couldn't launch anything that interfaced with the desktop as SUDO ... the solution was (or at least my solution) was to alter the tightVNCServer config file in the init.d directory and change the user from "Pi" to "Root".

I lost my desktop icons but it means that u can now run things as root if needed.

Once again many thanks everyone
Posts: 82
Joined: Thu May 31, 2012 7:56 pm
by Pskijj » Mon Nov 18, 2013 6:44 pm
I had the same problem when I was trying to run a GUI-program through tightvnc and in that case you should use gksudo instead of sudo
Posts: 5
Joined: Mon Oct 14, 2013 10:43 am
by nutshelluniverse » Sun Nov 24, 2013 6:02 pm
rickseiden wrote:I've always used IDLE, which is for Python 2, and not IDLE 3 which is for Python 3.

But if you do want to use Python 3.x for GPIO experimentation and RPi.GPIO is installed on Python 3 (as with me), you run 'sudo python3 programname.py' instead of 'sudo python programname.py' as the GPIO module will not be installed on Python 2.x
Posts: 83
Joined: Sun Nov 10, 2013 5:40 pm
Location: UK
by gordon77 » Sun Nov 24, 2013 8:25 pm
I use python 2.7 all the time and rpi.GPIO is installed.
Posts: 2847
Joined: Sun Aug 05, 2012 3:12 pm
by wakey7 » Wed Nov 27, 2013 11:10 am
Running as gksudo solved this for me. I am confused though as I have run GUI applications without root access before, but very recently I got the same error as you and had to run as gksudo instead of sudo.

It works fine though, thanks for the help.
Posts: 9
Joined: Thu Nov 14, 2013 1:49 pm