3

Differences between revisions 3 and 10 (spanning 7 versions)
Revision 3 as of 2012-04-20 17:17:04
Size: 1745
Editor: mail
Comment:
Revision 10 as of 2012-04-20 19:57:12
Size: 4822
Editor: mail
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
== 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 [[http://docs.python.org/py3k/library/2to3.html|2to3]] or the third party [[http://pypi.python.org/pypi/six|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 [[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]]]

== Python extension modules ==
Line 9: Line 42:
 * [[http://wiki.debian.org/Python/LibraryStyleGuide|Debian Python packaging style guide (covers Python 2 and Python 3)]]
Line 10: Line 44:
 * [[http://python3porting.com/|in-depth Python 3 porting guide]]  * '''Excellent''' [[http://python3porting.com/|in-depth Python 3 porting guide]]
Line 12: Line 46:
 * [[http://pypi.python.org/pypi?:action=browse&c=533&show=all|Cheeseshop packages explicitly claiming Python 3 support]]
Line 14: Line 49:
 * Barry Warsaw's blog
   * [[http://www.wefearchange.org/2012/01/debian-package-for-python-2-and-3.html|Debian packaging for Python 2 and 3]]
   * [[http://www.wefearchange.org/2011/12/lessons-in-porting-to-python-3.html|Python 3 porting (part 1)]]
   * [[http://www.wefearchange.org/2012/01/python-3-porting-fun-redux.html|Python 3 porting (part 2)]]
   * Python 3 plans for Ubuntu 12.10 (coming soon)
   * [[http://www.wefearchange.org/2011/11/update-on-ubuntus-python-plans.html|Python 3 plans for Ubuntu 12.04 Precise Pangolin]]
 * Ned Bachelder's Pycon 2012 talk [[http://pyvideo.org/video/948/pragmatic-unicode-or-how-do-i-stop-the-pain|Pragmatic Unicode, or How Do I Stop the Pain?]] '''''Watch this NOW'''''

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 localhost)