Page 1 of 2

STICKY: Python Usage Guide

Posted: Tue Jun 10, 2014 5:29 pm
by ben_nuttall
I've written a usage guide for getting started with Python: http://www.raspberrypi.org/documentation/usage/python

Re: Python Usage Guide

Posted: Sat Feb 07, 2015 11:43 am
by buckyb
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

Re: Python Usage Guide

Posted: Sat Feb 07, 2015 12:25 pm
by DirkS
The documentation is maintained on github.
I would create an issue there: https://github.com/raspberrypi/document ... /README.md

Re: Python Usage Guide

Posted: Wed Mar 11, 2015 9:57 pm
by MisterEShopper
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?

Re: Python Usage Guide

Posted: Thu Mar 12, 2015 4:21 am
by rpdom
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.

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 11:07 am
by TerryC65
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?

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 11:24 am
by DougieLawson
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

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 11:33 am
by joan
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.

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 12:27 pm
by TerryC65
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-) )

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 12:41 pm
by DougieLawson
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.

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 12:57 pm
by joan
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.

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 3:29 pm
by Douglas6
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?

Re: Python Usage Guide - Using GPIO

Posted: Tue Jun 09, 2015 3:49 pm
by joan
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.

Re: Python Usage Guide

Posted: Tue Jun 09, 2015 4:28 pm
by croston
I am currently writing a mechanism for RPi.GPIO not to require root permissions.

Re: Python Usage Guide

Posted: Tue Jun 09, 2015 5:39 pm
by DougieLawson
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.

Re: Python Usage Guide

Posted: Wed Jun 10, 2015 8:24 am
by TerryC65
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.

Re: Python Usage Guide

Posted: Wed Jul 22, 2015 1:29 pm
by ugnelakys
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

Re: Python Usage Guide

Posted: Mon Sep 28, 2015 9:07 pm
by pirat3k
Can you tell me how to connect mysql?
I don't know how to install the package (mysql_connector or something like that)

Re: Python Usage Guide

Posted: Fri May 06, 2016 6:45 pm
by direfulfoil23
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.

Re: Python Usage Guide

Posted: Fri May 06, 2016 7:28 pm
by DirkS
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.

Re: Python Usage Guide

Posted: Fri May 06, 2016 7:41 pm
by dgordon42
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.

Re: Python Usage Guide

Posted: Fri May 06, 2016 7:59 pm
by B.Goode
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.)

Re: Python Usage Guide

Posted: Fri Nov 11, 2016 2:00 pm
by VladGets

Re: Python Usage Guide

Posted: Thu Dec 22, 2016 4:58 pm
by IT IS
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

Re: Python Usage Guide

Posted: Mon May 15, 2017 6:33 am
by gkreidl
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/