Wednesday, September 12, 2012

Raspberry PI - Python 2 and 3 - Print, Serial, GPIO

As a Python newbie, I am stumbling upon things about it as I go along.

One of the things that I have discovered is that in Python 2, you can import the serial function that is used in Python 3 using the following:

from __future__ import print_function

then you can use the Python 3 print function in Python 2 programs

print(*args, sep=' ', end='\n', file=None)

From what I am reading (I am a real Python newbie) for the beginning programmer the biggest difference between 2 and 3 is the the way print works - in 2 it is a statement, in 3 it is a function.

Also, it looks like you can install the important Serial and GPIO support modules to work under Python 2 or 3. I assume that the module install routine detects what version it is being installed for and does the "right thing".

For Serial see:

For GPIO see:

I have not really experimented with this yet, but it would mean that you should be able to do most common programming using Python 3, which from what I am reading, is the preferred version to use.

Apparently the only real reason to use 2 is if there is some module you absolutely need that just does not work in 3 (yet).

Eric Pierce VA3EP - See the Disclaimer in the Introduction

© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.

No comments:

Post a Comment