3

Differences between revisions 8 and 10 (spanning 2 versions)
Revision 8 as of 2012-04-20 18:51:27
Size: 3825
Editor: barry
Comment:
Revision 10 as of 2012-04-20 19:57:12
Size: 4822
Editor: barry
Comment:
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
 * Do not rely on [[http://docs.python.org/py3k/library/2to3.html|2to3]]  * Do not rely on [[http://docs.python.org/py3k/library/2to3.html|2to3]] or the third party [[http://pypi.python.org/pypi/six|six]] module.
Line 21: Line 21:
=== 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 [[http://python3porting.com/differences.html#imports|[more info]]]
 * {{{print()}}} is a function in Python 3 [[http://python3porting.com/noconv.html#print-section|[more info]]]
 * Unadorned string literals are unicode type in Python 3 [[http://python3porting.com/problems.html#binary-section|[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 [[http://python3porting.com/problems.html#byte-literals|[more info]]]
Line 27: Line 44:
 * [[http://python3porting.com/|in-depth Python 3 porting guide]]  * '''Excellent''' [[http://python3porting.com/|in-depth Python 3 porting guide]]

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.
  • Do not rely on 2to3 or the third party six module.

  • 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 source

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

Resources

Python/3 (last edited 2016-02-26 01:40:43 by cmawebsite)