Python 3 on Ubuntu
It is a release goal for Ubuntu 12.10 to have only Python 3 on the installation CD images. We have a Q-series blueprint for discussion of this goal at UDS-Q in Oakland, California, in May of 2012. There is a more detailed spec for this effort and a publicly shared Google docs spreadsheet to track this effort. This is an ambitious effort that will only succeed with help from the greater Ubuntu, Debian, and Python communities. In other words, we need you!
At the bottom of this page, you will find various resources for diving more into aspects of supporting Python 3, from the pure-Python, C extension module, Debian packaging, and other perspectives. The intent of this page is to provide specific guidelines in a quick reference format, so that you only need to go here once you're familiar with the basic concepts and approaches, but need a refresher on specific coding techniques. This is a wiki page, and you are encouraged to contribute, but try to keep your recommendations tightly focused on accomplishing the release goal of Python 3 only on the 12.10 CDs.
Before you start
Here are recommendations for you to follow before you start porting.
- Target Python 3.2, 2.7, and optionally 2.6. Ignore anything older than that.
- Use a single code base for both Python 2 and 3.
- Modernize your Python 2 code first, getting it working in Python 2.7 before starting to port.
- Clarify your data model: what are bytes (data) and what are strings (text)?
I cannot overemphasize the last point. Without a clear separation in your mind and data model between bytes and strings, your port will likely be much more painful than it needs to be. This is the biggest distinction between Python 2 and Python 3. Where Python 2 let you be sloppy, with its 8-bit strings that served as both data and ASCII strings, with automatic (but error prone) conversions between 8-bit strings and unicodes, in Python 3 there are only bytes and strings (i.e. unicodes), with no automatic conversion between the two. This is A Good Thing.
Python 3 file compatibility
Put the following at the top of all your Python files:
from __future__ import absolute_import, print_function, unicode_literals
This turns on three important compatibility flags.
Absolute imports are the default in Python 3 [more info]
print() is a function in Python 3 [more info]
Unadorned string literals are unicode type in Python 3 [more info]
Now, change all your print statements to use print() functions, and remove all the u'' prefixes from your strings.
Also, if you have string literals in your code that represent data, prefix them all with the b'' prefix [more info]
Python extension modules
Excellent in-depth Python 3 porting guide
- Barry Warsaw's blog
Ned Bachelder's Pycon 2012 talk Pragmatic Unicode, or How Do I Stop the Pain? Watch this NOW