@@ -20,6 +20,7 @@ Porting Python 2 Code to Python 3
2020
2121Choosing a Strategy
2222===================
23+
2324When a project makes the decision that it's time to support both Python 2 & 3,
2425a decision needs to be made as to how to go about accomplishing that goal.
2526The chosen strategy will depend on how large the project's existing
@@ -54,6 +55,7 @@ current best practices in a Python 2/3 compatible way.
5455
5556Universal Bits of Advice
5657------------------------
58+
5759Regardless of what strategy you pick, there are a few things you should
5860consider.
5961
@@ -128,6 +130,7 @@ and work from there.
128130
129131Python 3 and 3to2
130132=================
133+
131134If you are starting a new project or your codebase is small enough, you may
132135want to consider writing your code for Python 3 and backporting to Python 2
133136using 3to2 _. Thanks to Python 3 being more strict about things than Python 2
@@ -149,6 +152,7 @@ that 3to2 is not a high-quality project.
149152
150153Python 2 and 2to3
151154=================
155+
152156Included with Python since 2.6, the 2to3 _ tool (and :mod: `lib2to3 ` module)
153157helps with porting Python 2 to Python 3 by performing various source
154158translations. This is a perfect solution for projects which wish to branch
@@ -176,6 +180,7 @@ to supporting Python 2 & 3.
176180
177181Support Python 2.7
178182------------------
183+
179184As a first step, make sure that your project is compatible with `Python 2.7 `_.
180185This is just good to do as Python 2.7 is the last release of Python 2 and thus
181186will be used for a rather long time. It also allows for use of the ``-3 `` flag
@@ -184,6 +189,7 @@ known to cause issues.
184189
185190Try to Support `Python 2.6 `_ and Newer Only
186191-------------------------------------------
192+
187193While not possible for all projects, if you can support `Python 2.6 `_ and newer
188194**only **, your life will be much easier. Various future statements, stdlib
189195additions, etc. exist only in Python 2.6 and later which greatly assist in
@@ -199,6 +205,7 @@ at least need to watch out for situations that these solutions fix.
199205
200206``from __future__ import print_function ``
201207'''''''''''''''''''''''''''''''''''''''''
208+
202209This is a personal choice. 2to3 handles the translation from the print
203210statement to the print function rather well so this is an optional step. This
204211future statement does help, though, with getting used to typing
@@ -207,6 +214,7 @@ future statement does help, though, with getting used to typing
207214
208215``from __future__ import unicode_literals ``
209216'''''''''''''''''''''''''''''''''''''''''''
217+
210218Another personal choice. You can always mark what you want to be a (unicode)
211219string with a ``u `` prefix to get the same effect. But regardless of whether
212220you use this future statement or not, you **must ** make sure you know exactly
@@ -217,6 +225,7 @@ strings with a ``u`` prefix if you do not use this future statement.
217225
218226Bytes literals
219227''''''''''''''
228+
220229This is a **very ** important one. The ability to prefix Python 2 strings that
221230are meant to contain bytes with a ``b `` prefix help to very clearly delineate
222231what is and is not a Python 3 string. When you run 2to3 on code, all Python 2
@@ -238,12 +247,14 @@ also comes about when doing comparisons between bytes and strings.
238247
239248Supporting `Python 2.5 `_ and Newer Only
240249---------------------------------------
250+
241251If you are supporting `Python 2.5 `_ and newer there are still some features of
242252Python that you can utilize.
243253
244254
245255``from __future__ import absolute_imports ``
246256'''''''''''''''''''''''''''''''''''''''''''
257+
247258Implicit relative imports (e.g., importing ``spam.bacon `` from within
248259``spam.eggs `` with the statement ``import bacon ``) does not work in Python 3.
249260This future statement moves away from that and allows the use of explicit
@@ -261,13 +272,15 @@ than Python 2.5, use the __future__ statement.
261272
262273Handle Common "Gotchas"
263274-----------------------
275+
264276There are a few things that just consistently come up as sticking points for
265277people which 2to3 cannot handle automatically or can easily be done in Python 2
266278to help modernize your code.
267279
268280
269281``from __future__ import division ``
270282'''''''''''''''''''''''''''''''''''
283+
271284While the exact same outcome can be had by using the ``-Qnew `` argument to
272285Python, using this future statement lifts the requirement that your users use
273286the flag to get the expected behavior of division in Python 3
@@ -305,6 +318,7 @@ possibilities:
305318
306319Subclass ``object ``
307320'''''''''''''''''''
321+
308322New-style classes have been around since `Python 2.2 `_. You need to make sure
309323you are subclassing from ``object `` to avoid odd edge cases involving method
310324resolution order, etc. This continues to be totally valid in Python 3 (although
@@ -313,6 +327,7 @@ unneeded as all classes implicitly inherit from ``object``).
313327
314328Deal With the Bytes/String Dichotomy
315329''''''''''''''''''''''''''''''''''''
330+
316331One of the biggest issues people have when porting code to Python 3 is handling
317332the bytes/string dichotomy. Because Python 2 allowed the ``str `` type to hold
318333textual data, people have over the years been rather loose in their delineation
@@ -340,6 +355,7 @@ literals you can either use six's ``u()`` function or use a ``u`` prefix.
340355
341356Decide what APIs Will Accept
342357****************************
358+
343359In Python 2 it was very easy to accidentally create an API that accepted both
344360bytes and textual data. But in Python 3, thanks to the more strict handling of
345361disparate types, this loose usage of bytes and text together tends to fail.
@@ -417,9 +433,10 @@ bytes object instead of raising ``IndexError``:
417433
418434``__str__() ``/``__unicode__() ``
419435'''''''''''''''''''''''''''''''
436+
420437In Python 2, objects can specify both a string and unicode representation of
421438themselves. In Python 3, though, there is only a string representation. This
422- becomes an issue as people can inadvertantly do things in their ``__str__() ``
439+ becomes an issue as people can inadvertently do things in their ``__str__() ``
423440methods which have unpredictable results (e.g., infinite recursion if you
424441happen to use the ``unicode(self).encode('utf8') `` idiom as the body of your
425442``__str__() `` method).
@@ -484,6 +501,7 @@ friends.
484501
485502Updating doctests
486503'''''''''''''''''
504+
4875052to3 _ will attempt to generate fixes for doctests that it comes across. It's
488506not perfect, though. If you wrote a monolithic set of doctests (e.g., a single
489507docstring containing all of your doctests), you should at least consider
@@ -494,6 +512,7 @@ to :mod:`unittest`.
494512
495513Eliminate ``-3 `` Warnings
496514-------------------------
515+
497516When you run your application's test suite, run it using the ``-3 `` flag passed
498517to Python. This will cause various warnings to be raised during execution about
499518things that 2to3 cannot handle automatically (e.g., modules that have been
@@ -503,12 +522,14 @@ to Python 3.
503522
504523Run 2to3
505524--------
525+
506526Once you have made your Python 2 code future-compatible with Python 3, it's
507527time to use 2to3 _ to actually port your code.
508528
509529
510530Manually
511531''''''''
532+
512533To manually convert source code using 2to3 _, you use the ``2to3 `` script that
513534is installed with Python 2.6 and later.::
514535
526547
527548During Installation
528549'''''''''''''''''''
550+
529551When a user installs your project for Python 3, you can have either
530552:mod: `distutils ` or Distribute _ run 2to3 _ on your behalf.
531553For distutils, use the following idiom::
@@ -552,6 +574,7 @@ you at least build your project and use the built Python 3 source for testing.
552574
553575Verify & Test
554576-------------
577+
555578At this point you should (hopefully) have your project converted in such a way
556579that it works in Python 3. Verify it by running your unit tests and making sure
557580nothing has gone awry. If you miss something then figure out how to fix it in
@@ -567,6 +590,7 @@ to verify the fix transforms properly.
567590
568591Python 2/3 Compatible Source
569592============================
593+
570594While it may seem counter-intuitive, you can write Python code which is
571595source-compatible between Python 2 & 3. It does lead to code that is not
572596entirely idiomatic Python (e.g., having to extract the currently raised
@@ -589,6 +613,7 @@ the same source code.
589613
590614Follow The Steps for Using 2to3 _ (sans 2to3)
591615--------------------------------------------
616+
592617All of the steps outlined in how to
593618:ref: `port Python 2 code with 2to3 <use_2to3 >` apply
594619to creating a Python 2/3 codebase. This includes trying only support Python 2.6
@@ -602,6 +627,7 @@ code so that you can fix them properly before they become an issue.
602627
603628Use six _
604629--------
630+
605631The six _ project contains many things to help you write portable Python code.
606632You should make sure to read its documentation from beginning to end and use
607633any and all features it provides. That way you will minimize any mistakes you
@@ -610,6 +636,7 @@ might make in writing cross-version code.
610636
611637Capturing the Currently Raised Exception
612638----------------------------------------
639+
613640One change between Python 2 and 3 that will require changing how you code (if
614641you support `Python 2.5 `_ and earlier) is
615642accessing the currently raised exception. In Python 2.5 and earlier the syntax
@@ -658,9 +685,11 @@ likely don't need it.
658685 (e.g. the third element of the tuple returned by :func: `sys.exc_info `)
659686 in a variable.
660687
688+
661689Other Resources
662690===============
663- The authors of the following blogs posts and wiki pages deserve special thanks
691+
692+ The authors of the following blog posts and wiki pages deserve special thanks
664693for making public their tips for porting Python 2 code to Python 3 (and thus
665694helping provide information for this document):
666695
0 commit comments