Python Usage Guide


25 posts
by ben_nuttall » Tue Jun 10, 2014 5:29 pm
I've written a usage guide for getting started with Python: http://www.raspberrypi.org/documentation/usage/python
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 189
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
by buckyb » Sat Feb 07, 2015 11:43 am
Hi Ben

Just tried your Python examples with IDLE3. A number do not work as expected although they do with python in a terminal or with pyCharm on Ubuntu.

eg

Code: Select all
for i in range(2):
    print("A")
print("B")


That was with two new last issues of raspian on a cleaned SD card using a B model with 8GB SD from RPi store. Running a file, after saving, from IDLE3 also did not seem to work.

Am I missing something?

regards
Posts: 14
Joined: Sun Mar 30, 2014 3:05 pm
Location: South Africa
by DirkS » Sat Feb 07, 2015 12:25 pm
The documentation is maintained on github.
I would create an issue there: https://github.com/raspberrypi/document ... /README.md
Posts: 6790
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK
by MisterEShopper » Wed Mar 11, 2015 9:57 pm
Thank you for the instructions, but they do not include a HOWTO INSTALL Python and Idle to get started. How does one install Python and IDLE?
Posts: 1
Joined: Wed Mar 11, 2015 9:55 pm
by rpdom » Thu Mar 12, 2015 4:21 am
MisterEShopper wrote:Thank you for the instructions, but they do not include a HOWTO INSTALL Python and Idle to get started. How does one install Python and IDLE?

Are you using the recommended Raspbian OS?

If so, both Python and IDLE are already installed.
User avatar
Posts: 10696
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by TerryC65 » Tue Jun 09, 2015 11:07 am
Ben,
In your Advanced User Guide, under the heading 'GPIO' you state:
To control the GPIO pins you'll need root access, so run sudo python, sudo ipython or sudo idle &.

Can I ask why this is? It seems to make life difficult and seems to go against everything I've ever known about not running anything as root unless something is being installed or configured.

Also, I have been playing around with Python using IDLE and am therefore most confident when I have the ability to use the debugger. At the moment I cannot see a way to run my code without giving myself root privileges or starting IDLE with sudo. Neither of these seem 'quite right', so I wonder if there's another way?
User avatar
Posts: 54
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK
by DougieLawson » Tue Jun 09, 2015 11:24 am
TerryC65 wrote:Also, I have been playing around with Python using IDLE and am therefore most confident when I have the ability to use the debugger. At the moment I cannot see a way to run my code without giving myself root privileges or starting IDLE with sudo. Neither of these seem 'quite right', so I wonder if there's another way?

There is another way.

If your id is in the i2c group you can use I2C without root, if it's in the spi group you can use SPI without root and if it's in the gpio group you can use the /sys virtual filesystem to access GPIO pins without root.

The problem with /sys is that it's slow.

echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "1" > /sys/class/gpio/gpio17/value

And to drive that from python https://github.com/derekstavis/python-sysfs-gpio
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27907
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by joan » Tue Jun 09, 2015 11:33 am
TerryC65 wrote:Ben,
In your Advanced User Guide, under the heading 'GPIO' you state:
To control the GPIO pins you'll need root access, so run sudo python, sudo ipython or sudo idle &.

Can I ask why this is? It seems to make life difficult and seems to go against everything I've ever known about not running anything as root unless something is being installed or configured.

Also, I have been playing around with Python using IDLE and am therefore most confident when I have the ability to use the debugger. At the moment I cannot see a way to run my code without giving myself root privileges or starting IDLE with sudo. Neither of these seem 'quite right', so I wonder if there's another way?

You need root access to access memory and most modules access the gpios as memory mapped devices for performance reasons.

My pigpio Python module allows you to access the gpios without having to run Python as root.
User avatar
Posts: 12657
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by TerryC65 » Tue Jun 09, 2015 12:27 pm
joan wrote:You need root access to access memory and most modules access the gpios as memory mapped devices for performance reasons.

OK. So in other words the GPIO is directly mapped to memory to provide the best speed. Is that right?

That's why Dougie, in his earlier response says that putting the user into the GPIO group is slow, because access is then indirect, via /sys?

joan wrote:My pigpio Python module allows you to access the gpios without having to run Python as root.

So how does this provide the performance? (I'm not an experienced programmer, so can't work it out from the code 8-) )
User avatar
Posts: 54
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK
by DougieLawson » Tue Jun 09, 2015 12:41 pm
TerryC65 wrote:
joan wrote:My pigpio Python module allows you to access the gpios without having to run Python as root.

So how does this provide the performance? (I'm not an experienced programmer, so can't work it out from the code 8-) )


Because Joan's pigpiod is a single purpose dedicated server that reads your request from a non-privileged client, runs optimised code to wiggle/read/furgle with your GPIO pins and posts the result back to the requester. It's not a generic interface.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27907
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by joan » Tue Jun 09, 2015 12:57 pm
DougieLawson wrote:...
Because Joan's pigpiod is a single purpose dedicated server that reads your request from a non-privileged client, runs optimised code to wiggle/read/furgle with your GPIO pins and posts the result back to the requester. It's not a generic interface.

Given that we now have a gpio group it should be straightforward to write a generic Python module to manipulate the gpios. It should be possible to do all the basics such as input/output, software PWM, callbacks on level changes in Python without requiring root access. That alone would probably cover 95% of usage. Bit of an opening there for anyone who wants to write a library.
User avatar
Posts: 12657
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Douglas6 » Tue Jun 09, 2015 3:29 pm
joan wrote:Given that we now have a gpio group it should be straightforward to write a generic Python module to manipulate the gpios.... Bit of an opening there for anyone who wants to write a library.
Interesting. But what would it need to do differently than RPi.GPIO?
User avatar
Posts: 3812
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by joan » Tue Jun 09, 2015 3:49 pm
Douglas6 wrote:
joan wrote:Given that we now have a gpio group it should be straightforward to write a generic Python module to manipulate the gpios.... Bit of an opening there for anyone who wants to write a library.
Interesting. But what would it need to do differently than RPi.GPIO?

I am not sure. I may be missing something but I do not see why RPi.GPIO could not be updated so that it did not require root access. croston (the author) should know off the top of his head.
User avatar
Posts: 12657
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by croston » Tue Jun 09, 2015 4:28 pm
I am currently writing a mechanism for RPi.GPIO not to require root permissions.
User avatar
Posts: 657
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by DougieLawson » Tue Jun 09, 2015 5:39 pm
croston wrote:I am currently writing a mechanism for RPi.GPIO not to require root permissions.


Excellent, I'll be willing to beta test your code.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27907
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by TerryC65 » Wed Jun 10, 2015 8:24 am
croston wrote:I am currently writing a mechanism for RPi.GPIO not to require root permissions.

Excellent!

Also, thanks to Joan and Dougie for explaining things.
User avatar
Posts: 54
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK
by ugnelakys » Wed Jul 22, 2015 1:29 pm
ben_nuttall wrote:I've written a usage guide for getting started with Python: http://www.raspberrypi.org/documentation/usage/python


Don't forget the main manual; but i think it is for advanced learning https://docs.python.org/3.2/library/

Also some examples: Python examples
Posts: 1
Joined: Wed Jul 22, 2015 1:18 pm
by pirat3k » Mon Sep 28, 2015 9:07 pm
Can you tell me how to connect mysql?
I don't know how to install the package (mysql_connector or something like that)
Posts: 1
Joined: Mon Sep 28, 2015 9:02 pm
by direfulfoil23 » Fri May 06, 2016 6:45 pm
Hello,

I am using IDLE and am encountering a problem at times, an example would be when I try to write:
>>>for i in range(2):
print("A")
print("B")

How do I get the arrows to return, basically so I can end the loop and write something new? Sorry if I don't make much sense or use proper vernacular, I'm very new to this.
Posts: 40
Joined: Fri May 06, 2016 6:30 pm
by DirkS » Fri May 06, 2016 7:28 pm
direfulfoil23 wrote:Hello,

I am using IDLE and am encountering a problem at times, an example would be when I try to write:
>>>for i in range(2):
print("A")
print("B")

How do I get the arrows to return, basically so I can end the loop and write something new? Sorry if I don't make much sense or use proper vernacular, I'm very new to this.

leave line empty and press <ENTER>

BTW: in future please start a new topic for questions like this.
Posts: 6790
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK
by dgordon42 » Fri May 06, 2016 7:41 pm
After you have entered all the code for the loop and pressed <Return>, press <Backspace> to loose the indent and get out of the loop.

Also, when posting Python code on the forums indentation is important, so after pasting your code highlight it and press the "Code" button at the top if the editor. That way, we can see the indentation, and make more sense of your code. You are more likely to get questions answered that way.

Hope this helps,
Dave.
Posts: 653
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland
by B.Goode » Fri May 06, 2016 7:59 pm
direfulfoil23 wrote:Hello,

I am using IDLE and am encountering a problem at times, an example would be when I try to write:
>>>for i in range(2):
print("A")
print("B")

How do I get the arrows to return, basically so I can end the loop and write something new? Sorry if I don't make much sense or use proper vernacular, I'm very new to this.


Before you write a lot of code and get very frustrated, please be aware that IDLE's shell window (identified by the '>>> ' prompt from the interactive python interpreter) does not allow you to (easily) save, reload and rerun your script.

You might want to look ahead in the guide to the section Python files in IDLE to see how to overcome this restriction by creating a new, blank, file into which you type your script.

The interactive shell window is a great tool for quick 'one-shot' experiments, but it is not a useful place for writing and saving permanent scripts. I think the Python Usage Guide which you seem to be following makes this clear eventually.

(If you are following the Python Usage Guide referenced in the first post in this topic then I see no reason why you should not have raised a query about it here.)
Posts: 3534
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by VladGets » Fri Nov 11, 2016 2:00 pm
Posts: 1
Joined: Fri Nov 11, 2016 1:58 pm
by IT IS » Thu Dec 22, 2016 4:58 pm
Wow, thank you for that guide.
I haven't read all the comments, but it seems to me as an python beginner that python could be little easier to learn than java or c++.

I'm looking forward to my first projekt :D
Posts: 1
Joined: Thu Dec 22, 2016 4:41 pm
by gkreidl » Mon May 15, 2017 6:33 am
AshPowers wrote:I would like to compile my program into a binary executable as it will be part of a product and do not want the source code being so easily obtainable.

What is the best method of doing this?

Thanks!

Nuitka.
http://nuitka.net/
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer
Posts: 5026
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany