We are writing our APIs for Python 3. Python 3 a relatively new version of the language and is not entirely backwards compatible with Python 2. A number of libraries have not yet been ported to this new version. But we believe Python 3 is a good choice for programming our educational kits because it removes many confusing aspects of Python 2.

Why Python 3?

We selected Python 3 for a number of reasons:

  1. We’re developing educational kits and Python 3 is the Python version selected for the Raspberry Pi educational release.
  2. Python 3 makes a clear distinction between byte buffers and text strings, which makes doing I/O, working with raw data and working with text strings easier than in Python 2. Python 2 does have a bytes function to create byte buffers that has the same syntax as that of Python 3. However, it has different and confusing behaviour. For example, in Python 3 it acts as you’d expect:
    Python 3.1.2 (release31-maint, Dec  9 2011, 20:50:50) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> bytes([1,2,3]) == b'\x01\x02\x03'
    True

    But in Python 2:

    Python 2.7.0+ (r27:82500, May  9 2011, 20:29:29) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> bytes([1,2,3]) == b'\x01\x02\x03'
    False
    >>> bytes([1,2,3]) == b'\x5b\x31\x2c\x20\x32\x2c\x20\x33\x5d'
    True

    Can you work out why?

  3. Python 3 has only one, universal, type system/object model, and so avoids confusing errors that can happen in Python 2 when you accidentally forget to declare that your class extends object and then try to use an API that uses the binding protocol or metaclasses. If that last sentence sounds like total gibberish, that demonstrates why we’re using Python 3! We shouldn’t have to expose those advanced language features to new programmers but it’s easy to bump into them by accident in Python 2.
  4. Many other small quirks and annoyances that remain in Python 2 to ensure backward compatability have been removed, making the language and standard library easier to use.

Unfortunately some popular libraries have not yet been ported to Python 3, including (at the time of writing):

Happily, more libraries are becoming available as time goes by. Those above may have been ported by the time you read this.

Using Python 3 on the Raspberry Pi

Python 3 is not included in the default Raspberry Pi disk image at the moment. You’ll need to install it with the command:

sudo apt-get install python3

Then you use the python3 command to run Python 3 scripts:

python3 my-script.py

To make an executable Python script run with Python 3, the script should start with the following line:

#!/usr/bin/env python3

For example, the following script should report that it is running Python 3:

#!/usr/bin/env python3

import sys

print(sys.version)

About the author: Nat Pryce

3 Comments

  1. What is the best way to learn python and how to write programs to be able to input physical data and output to more than just a screen using the r pi as the computer: when you are 66 and know little.
    books coursesetc.

    Reply

  2. There’s a lot of material out there about learning python. Good places to start might be the new RaspberryPi book which I believe is for beginners. There are also some good tips in the RasPi online magazine. Google has a tutorial at
    http://code.google.com/edu/languages/google-python-class/

    and there’s material from the python website itself at http://www.python.org

    As for the age thing, I’m hoping that it doesn’t matter :)

    Reply

  3. On my PC I have to use Python2 because wxPython doesn’t support 3. Is there any chance of a Python 2 version of the quick2wire libs? Or at least one that supports both 2 and 3? Particularly dual support would be very interesting!

    BTW wxPython is not the only package that doesn’t support 3, many of the scientific packages also seem to stick with 2. Too bad that breaking old code is tearing the Python community in two!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>