- ben_nuttall
- Posts: 258
- Joined: Sun Aug 19, 2012 11:19 am
STICKY: Python Usage Guide
I've written a usage guide for getting started with Python: http://www.raspberrypi.org/documentation/usage/python
Former RPF staff. Author of gpiozero and creator of piwheels.
Re: Python Usage Guide
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
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
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")Am I missing something?
regards
Re: Python Usage Guide
The documentation is maintained on github.
I would create an issue there: https://github.com/raspberrypi/document ... /README.md
I would create an issue there: https://github.com/raspberrypi/document ... /README.md
- MisterEShopper
- Posts: 1
- Joined: Wed Mar 11, 2015 9:55 pm
Re: Python Usage Guide
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
Are you using the recommended Raspbian OS?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?
If so, both Python and IDLE are already installed.
Re: Python Usage Guide - Using GPIO
Ben,
In your Advanced User Guide, under the heading 'GPIO' you state:
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?
In your Advanced User Guide, under the heading 'GPIO' you state:
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.To control the GPIO pins you'll need root access, so run sudo python, sudo ipython or sudo idle &.
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?
- DougieLawson
- Posts: 43604
- Joined: Sun Jun 16, 2013 11:19 pm
Re: Python Usage Guide - Using GPIO
There is another way.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?
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
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: Python Usage Guide - Using GPIO
You need root access to access memory and most modules access the gpios as memory mapped devices for performance reasons.TerryC65 wrote:Ben,
In your Advanced User Guide, under the heading 'GPIO' you state: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.To control the GPIO pins you'll need root access, so run sudo python, sudo ipython or sudo idle &.
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?
My pigpio Python module allows you to access the gpios without having to run Python as root.
Re: Python Usage Guide - Using GPIO
OK. So in other words the GPIO is directly mapped to memory to provide the best speed. Is that right?joan wrote:You need root access to access memory and most modules access the gpios as memory mapped devices for performance reasons.
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?
So how does this provide the performance? (I'm not an experienced programmer, so can't work it out from the code 8-) )joan wrote:My pigpio Python module allows you to access the gpios without having to run Python as root.
- DougieLawson
- Posts: 43604
- Joined: Sun Jun 16, 2013 11:19 pm
Re: Python Usage Guide - Using GPIO
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.TerryC65 wrote:So how does this provide the performance? (I'm not an experienced programmer, so can't work it out from the code 8-) )joan wrote:My pigpio Python module allows you to access the gpios without having to run Python as root.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: Python Usage Guide - Using GPIO
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.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.
Re: Python Usage Guide - Using GPIO
Interesting. But what would it need to do differently than RPi.GPIO?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.
Re: Python Usage Guide - Using 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.Douglas6 wrote:Interesting. But what would it need to do differently than RPi.GPIO?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.
Re: Python Usage Guide
I am currently writing a mechanism for RPi.GPIO not to require root permissions.
- DougieLawson
- Posts: 43604
- Joined: Sun Jun 16, 2013 11:19 pm
Re: Python Usage Guide
Excellent, I'll be willing to beta test your code.croston wrote:I am currently writing a mechanism for RPi.GPIO not to require root permissions.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: Python Usage Guide
Excellent!croston wrote:I am currently writing a mechanism for RPi.GPIO not to require root permissions.
Also, thanks to Joan and Dougie for explaining things.
Re: Python Usage Guide
Don't forget the main manual; but i think it is for advanced learning https://docs.python.org/3.2/library/ben_nuttall wrote:I've written a usage guide for getting started with Python: http://www.raspberrypi.org/documentation/usage/python
Also some examples: Python examples
Re: Python Usage Guide
Can you tell me how to connect mysql?
I don't know how to install the package (mysql_connector or something like that)
I don't know how to install the package (mysql_connector or something like that)
- direfulfoil23
- Posts: 40
- Joined: Fri May 06, 2016 6:30 pm
Re: Python Usage Guide
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.
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
leave line empty and press <ENTER>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.
BTW: in future please start a new topic for questions like this.
Re: Python Usage Guide
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.
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
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.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.
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.)
Beware of the Leopard
Re: Python Usage Guide
Read this blog
He helped you http://djangostars.com/blog/python-in-a-1000-words/
He helped you http://djangostars.com/blog/python-in-a-1000-words/
Re: Python Usage Guide
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
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
Nuitka.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!
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
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
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion