blob: ebee60fddb973f7ca7c587f924d403326e8bbb95 [file] [log] [blame]
Brett Cannon8045d972011-02-03 22:01:54 +00001.. _pyporting-howto:
2
3*********************************
4Porting Python 2 Code to Python 3
5*********************************
6
7:author: Brett Cannon
8
9.. topic:: Abstract
10
Brett Cannon4b0c24a2011-02-03 22:14:58 +000011 With Python 3 being the future of Python while Python 2 is still in active
12 use, it is good to have your project available for both major releases of
Brett Cannon9ca21b32014-01-07 11:52:04 -050013 Python. This guide is meant to help you figure out how best to support both
14 Python 2 & 3 simultaneously.
Brett Cannon8045d972011-02-03 22:01:54 +000015
Brett Cannon4b0c24a2011-02-03 22:14:58 +000016 If you are looking to port an extension module instead of pure Python code,
Éric Araujo5405a0b2011-02-05 16:03:12 +000017 please see :ref:`cporting-howto`.
Brett Cannon8045d972011-02-03 22:01:54 +000018
Brett Cannon9ca21b32014-01-07 11:52:04 -050019 If you would like to read one core Python developer's take on why Python 3
20 came into existence, you can read Nick Coghlan's `Python 3 Q & A`_.
Brett Cannon8045d972011-02-03 22:01:54 +000021
Brett Cannon9ca21b32014-01-07 11:52:04 -050022 If you prefer to read a (free) book on porting a project to Python 3,
23 consider reading `Porting to Python 3`_ by Lennart Regebro which should cover
24 much of what is discussed in this HOWTO.
Georg Brandl2cb2fa92011-02-07 15:30:45 +000025
Brett Cannon9ca21b32014-01-07 11:52:04 -050026 For help with porting, you can email the python-porting_ mailing list with
27 questions.
Brett Cannon8045d972011-02-03 22:01:54 +000028
29
Brett Cannon9ca21b32014-01-07 11:52:04 -050030Before You Begin
31================
Georg Brandl2cb2fa92011-02-07 15:30:45 +000032
Brett Cannon9ca21b32014-01-07 11:52:04 -050033If your project is on the Cheeseshop_/PyPI_, make sure it has the proper
34`trove classifiers`_ to signify what versions of Python it **currently**
35supports. At minimum you should specify the major version(s), e.g.
36``Programming Language :: Python :: 2`` if your project currently only supports
37Python 2. It is preferrable that you be as specific as possible by listing every
38major/minor version of Python that you support, e.g. if your project supports
39Python 2.6 and 2.7, then you want the classifiers of::
Brett Cannon8045d972011-02-03 22:01:54 +000040
Brett Cannon9ca21b32014-01-07 11:52:04 -050041 Programming Language :: Python :: 2
42 Programming Language :: Python :: 2.6
43 Programming Language :: Python :: 2.7
Brett Cannon8045d972011-02-03 22:01:54 +000044
Brett Cannon9ca21b32014-01-07 11:52:04 -050045Once your project supports Python 3 you will want to go back and add the
46appropriate classifiers for Python 3 as well. This is important as setting the
47``Programming Language :: Python :: 3`` classifier will lead to your project
48being listed under the `Python 3 Packages`_ section of PyPI.
Brett Cannon8045d972011-02-03 22:01:54 +000049
Brett Cannon9ca21b32014-01-07 11:52:04 -050050Make sure you have a robust test suite. You need to
51make sure everything continues to work, just like when you support a new
52minor/feature release of Python. This means making sure your test suite is
53thorough and is ported properly between Python 2 & 3 (consider using coverage_
54to measure that you have effective test coverage). You will also most likely
55want to use something like tox_ to automate testing between all of your
56supported versions of Python. You will also want to **port your tests first** so
57that you can make sure that you detect breakage during the transition. Tests also
58tend to be simpler than the code they are testing so it gives you an idea of how
59easy it can be to port code.
Brett Cannon8045d972011-02-03 22:01:54 +000060
Brett Cannon9ca21b32014-01-07 11:52:04 -050061Drop support for older Python versions if possible. `Python 2.5`_
Eli Bendersky2d062de2011-02-07 04:19:57 +000062introduced a lot of useful syntax and libraries which have become idiomatic
63in Python 3. `Python 2.6`_ introduced future statements which makes
64compatibility much easier if you are going from Python 2 to 3.
Brett Cannon9ca21b32014-01-07 11:52:04 -050065`Python 2.7`_ continues the trend in the stdlib. Choose the newest version
Eli Bendersky2d062de2011-02-07 04:19:57 +000066of Python which you believe can be your minimum support version
Brett Cannon8045d972011-02-03 22:01:54 +000067and work from there.
68
Brett Cannon9ca21b32014-01-07 11:52:04 -050069Target the newest version of Python 3 that you can. Beyond just the usual
Brett Cannonb7e6b892013-03-09 14:22:35 -050070bugfixes, compatibility has continued to improve between Python 2 and 3 as time
Brett Cannon9ca21b32014-01-07 11:52:04 -050071has passed. E.g. Python 3.3 added back the ``u`` prefix for
72strings, making source-compatible Python code easier to write.
Brett Cannonb7e6b892013-03-09 14:22:35 -050073
Brett Cannon8045d972011-02-03 22:01:54 +000074
Brett Cannon9ca21b32014-01-07 11:52:04 -050075Writing Source-Compatible Python 2/3 Code
76=========================================
77
78Over the years the Python community has discovered that the easiest way to
79support both Python 2 and 3 in parallel is to write Python code that works in
80either version. While this might sound counter-intuitive at first, it actually
81is not difficult and typically only requires following some select
82(non-idiomatic) practices and using some key projects to help make bridging
83between Python 2 and 3 easier.
84
85Projects to Consider
86--------------------
87
88The lowest level library for suppoting Python 2 & 3 simultaneously is six_.
89Reading through its documentation will give you an idea of where exactly the
90Python language changed between versions 2 & 3 and thus what you will want the
91library to help you continue to support.
92
93To help automate porting your code over to using six, you can use
94modernize_. This project will attempt to rewrite your code to be as modern as
95possible while using six to smooth out any differences between Python 2 & 3.
96
97If you want to write your compatible code to feel more like Python 3 there is
98the future_ project. It tries to provide backports of objects from Python 3 so
99that you can use them from Python 2-compatible code, e.g. replacing the
100``bytes`` type from Python 2 with the one from Python 3.
101It also provides a translation script like modernize (its translation code is
102actually partially based on it) to help start working with a pre-existing code
103base. It is also unique in that its translation script will also port Python 3
104code backwards as well as Python 2 code forwards.
Brett Cannon8045d972011-02-03 22:01:54 +0000105
106
Brett Cannon9ca21b32014-01-07 11:52:04 -0500107Tips & Tricks
108-------------
Brett Cannon8045d972011-02-03 22:01:54 +0000109
Brett Cannon9ca21b32014-01-07 11:52:04 -0500110To help with writing source-compatible code using one of the projects mentioned
111in `Projects to Consider`_, consider following the below suggestions. Some of
112them are handled by the suggested projects, so if you do use one of them then
113read their documentation first to see which suggestions below will taken care of
114for you.
Brett Cannon8045d972011-02-03 22:01:54 +0000115
116Support Python 2.7
Brett Cannon9ca21b32014-01-07 11:52:04 -0500117//////////////////
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000118
Brett Cannon8045d972011-02-03 22:01:54 +0000119As a first step, make sure that your project is compatible with `Python 2.7`_.
120This is just good to do as Python 2.7 is the last release of Python 2 and thus
121will be used for a rather long time. It also allows for use of the ``-3`` flag
Brett Cannon9ca21b32014-01-07 11:52:04 -0500122to Python to help discover places in your code where compatibility might be an
123issue (the ``-3`` flag is in Python 2.6 but Python 2.7 adds more warnings).
Brett Cannon8045d972011-02-03 22:01:54 +0000124
Brett Cannonce71ab22011-02-05 22:05:05 +0000125Try to Support `Python 2.6`_ and Newer Only
Brett Cannon9ca21b32014-01-07 11:52:04 -0500126///////////////////////////////////////////
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000127
Brett Cannon8045d972011-02-03 22:01:54 +0000128While not possible for all projects, if you can support `Python 2.6`_ and newer
129**only**, your life will be much easier. Various future statements, stdlib
130additions, etc. exist only in Python 2.6 and later which greatly assist in
Brett Cannon9ca21b32014-01-07 11:52:04 -0500131supporting Python 3. But if you project must keep support for `Python 2.5`_ then
132it is still possible to simultaneously support Python 3.
Brett Cannon8045d972011-02-03 22:01:54 +0000133
134Below are the benefits you gain if you only have to support Python 2.6 and
135newer. Some of these options are personal choice while others are
136**strongly** recommended (the ones that are more for personal choice are
137labeled as such). If you continue to support older versions of Python then you
Brett Cannon9ca21b32014-01-07 11:52:04 -0500138at least need to watch out for situations that these solutions fix and handle
139them appropriately (which is where library help from e.g. six_ comes in handy).
Brett Cannon8045d972011-02-03 22:01:54 +0000140
141
Brett Cannon8045d972011-02-03 22:01:54 +0000142``from __future__ import print_function``
143'''''''''''''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000144
Brett Cannon9ca21b32014-01-07 11:52:04 -0500145It will not only get you used to typing ``print()`` as a function instead of a
146statement, but it will also give you the various benefits the function has over
147the Python 2 statement (six_ provides a function if you support Python 2.5 or
148older).
Brett Cannon8045d972011-02-03 22:01:54 +0000149
150
151``from __future__ import unicode_literals``
152'''''''''''''''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000153
Brett Cannon9ca21b32014-01-07 11:52:04 -0500154If you choose not to use this future statement you should then mark all of your
155text strings with a ``u`` prefix and only support Python 3.3 or newer. But you
156are **strongly** advised to do one or the other (six_ provides a function in
157case you don't want to use the future statement **and** you want to support
158Python 3.2 or older).
Brett Cannon8045d972011-02-03 22:01:54 +0000159
160
161Bytes literals
162''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000163
Brett Cannon9ca21b32014-01-07 11:52:04 -0500164This is a **very** important one. Prefix Python 2 strings that
165are meant to contain bytes with a ``b`` prefix to very clearly delineate
166what is and is not a Python 3 text string (six_ provides a function to use for
167Python 2.5 compatibility).
Brett Cannon8045d972011-02-03 22:01:54 +0000168
Brett Cannonb7e6b892013-03-09 14:22:35 -0500169This point cannot be stressed enough: make sure you know what all of your string
Brett Cannon9ca21b32014-01-07 11:52:04 -0500170literals in Python 2 are meant to be in Python 3. Any string literal that
Brett Cannonb7e6b892013-03-09 14:22:35 -0500171should be treated as bytes should have the ``b`` prefix. Any string literal
172that should be Unicode/text in Python 2 should either have the ``u`` literal
173(supported, but ignored, in Python 3.3 and later) or you should have
174``from __future__ import unicode_literals`` at the top of the file. But the key
Brett Cannon9ca21b32014-01-07 11:52:04 -0500175point is you should know how Python 3 will treat every one one of your string
Brett Cannonb7e6b892013-03-09 14:22:35 -0500176literals and you should mark them as appropriate.
177
Brett Cannon8045d972011-02-03 22:01:54 +0000178There are some differences between byte literals in Python 2 and those in
179Python 3 thanks to the bytes type just being an alias to ``str`` in Python 2.
Brett Cannon9ca21b32014-01-07 11:52:04 -0500180See the `Handle Common "Gotchas"`_ section for what to watch out for.
Brett Cannon8045d972011-02-03 22:01:54 +0000181
Brett Cannon9ca21b32014-01-07 11:52:04 -0500182``from __future__ import absolute_import``
183''''''''''''''''''''''''''''''''''''''''''
184Discussed in more detail below, but you should use this future statement to
185prevent yourself from accidentally using implicit relative imports.
Brett Cannon8045d972011-02-03 22:01:54 +0000186
187
Brett Cannonce71ab22011-02-05 22:05:05 +0000188Supporting `Python 2.5`_ and Newer Only
Brett Cannon9ca21b32014-01-07 11:52:04 -0500189///////////////////////////////////////
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000190
Brett Cannonce71ab22011-02-05 22:05:05 +0000191If you are supporting `Python 2.5`_ and newer there are still some features of
192Python that you can utilize.
193
194
Ezio Melottic17c1f62011-04-21 14:49:03 +0300195``from __future__ import absolute_import``
196''''''''''''''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000197
Brett Cannonce71ab22011-02-05 22:05:05 +0000198Implicit relative imports (e.g., importing ``spam.bacon`` from within
Brett Cannon9ca21b32014-01-07 11:52:04 -0500199``spam.eggs`` with the statement ``import bacon``) do not work in Python 3.
Brett Cannonce71ab22011-02-05 22:05:05 +0000200This future statement moves away from that and allows the use of explicit
201relative imports (e.g., ``from . import bacon``).
202
203In `Python 2.5`_ you must use
204the __future__ statement to get to use explicit relative imports and prevent
205implicit ones. In `Python 2.6`_ explicit relative imports are available without
206the statement, but you still want the __future__ statement to prevent implicit
207relative imports. In `Python 2.7`_ the __future__ statement is not needed. In
208other words, unless you are only supporting Python 2.7 or a version earlier
Brett Cannon9ca21b32014-01-07 11:52:04 -0500209than Python 2.5, use this __future__ statement.
Brett Cannonce71ab22011-02-05 22:05:05 +0000210
211
Brett Cannonb7e6b892013-03-09 14:22:35 -0500212Mark all Unicode strings with a ``u`` prefix
213'''''''''''''''''''''''''''''''''''''''''''''
214
215While Python 2.6 has a ``__future__`` statement to automatically cause Python 2
216to treat all string literals as Unicode, Python 2.5 does not have that shortcut.
217This means you should go through and mark all string literals with a ``u``
Brett Cannon9ca21b32014-01-07 11:52:04 -0500218prefix to turn them explicitly into text strings where appropriate and only
219support Python 3.3 or newer. Otherwise use a project like six_ which provides a
220function to pass all text string literals through.
Brett Cannonb7e6b892013-03-09 14:22:35 -0500221
222
Brett Cannon9ca21b32014-01-07 11:52:04 -0500223Capturing the Currently Raised Exception
224''''''''''''''''''''''''''''''''''''''''
225
226In Python 2.5 and earlier the syntax to access the current exception is::
227
228 try:
229 raise Exception()
230 except Exception, exc:
231 # Current exception is 'exc'.
232 pass
233
234This syntax changed in Python 3 (and backported to `Python 2.6`_ and later)
235to::
236
237 try:
238 raise Exception()
239 except Exception as exc:
240 # Current exception is 'exc'.
241 # In Python 3, 'exc' is restricted to the block; in Python 2.6/2.7 it will "leak".
242 pass
243
244Because of this syntax change you must change how you capture the current
245exception in Python 2.5 and earlier to::
246
247 try:
248 raise Exception()
249 except Exception:
250 import sys
251 exc = sys.exc_info()[1]
252 # Current exception is 'exc'.
253 pass
254
255You can get more information about the raised exception from
256:func:`sys.exc_info` than simply the current exception instance, but you most
257likely don't need it.
258
259.. note::
260 In Python 3, the traceback is attached to the exception instance
261 through the ``__traceback__`` attribute. If the instance is saved in
262 a local variable that persists outside of the ``except`` block, the
263 traceback will create a reference cycle with the current frame and its
264 dictionary of local variables. This will delay reclaiming dead
265 resources until the next cyclic :term:`garbage collection` pass.
266
267 In Python 2, this problem only occurs if you save the traceback itself
268 (e.g. the third element of the tuple returned by :func:`sys.exc_info`)
269 in a variable.
270
Brett Cannonce71ab22011-02-05 22:05:05 +0000271
Brett Cannon8045d972011-02-03 22:01:54 +0000272Handle Common "Gotchas"
Brett Cannon9ca21b32014-01-07 11:52:04 -0500273///////////////////////
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000274
Brett Cannon9ca21b32014-01-07 11:52:04 -0500275These are things to watch out for no matter what version of Python 2 you are
276supporting which are not syntactic considerations.
Brett Cannon8045d972011-02-03 22:01:54 +0000277
278
Brett Cannonce71ab22011-02-05 22:05:05 +0000279``from __future__ import division``
280'''''''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000281
Brett Cannonce71ab22011-02-05 22:05:05 +0000282While the exact same outcome can be had by using the ``-Qnew`` argument to
283Python, using this future statement lifts the requirement that your users use
284the flag to get the expected behavior of division in Python 3
285(e.g., ``1/2 == 0.5; 1//2 == 0``).
286
287
288
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000289Specify when opening a file as binary
290'''''''''''''''''''''''''''''''''''''
291
292Unless you have been working on Windows, there is a chance you have not always
293bothered to add the ``b`` mode when opening a binary file (e.g., ``rb`` for
294binary reading). Under Python 3, binary files and text files are clearly
295distinct and mutually incompatible; see the :mod:`io` module for details.
296Therefore, you **must** make a decision of whether a file will be used for
297binary access (allowing to read and/or write bytes data) or text access
298(allowing to read and/or write unicode data).
299
300Text files
301''''''''''
302
303Text files created using ``open()`` under Python 2 return byte strings,
304while under Python 3 they return unicode strings. Depending on your porting
305strategy, this can be an issue.
306
307If you want text files to return unicode strings in Python 2, you have two
308possibilities:
309
310* Under Python 2.6 and higher, use :func:`io.open`. Since :func:`io.open`
311 is essentially the same function in both Python 2 and Python 3, it will
312 help iron out any issues that might arise.
313
314* If pre-2.6 compatibility is needed, then you should use :func:`codecs.open`
315 instead. This will make sure that you get back unicode strings in Python 2.
316
Brett Cannon8045d972011-02-03 22:01:54 +0000317Subclass ``object``
318'''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000319
Brett Cannonce71ab22011-02-05 22:05:05 +0000320New-style classes have been around since `Python 2.2`_. You need to make sure
321you are subclassing from ``object`` to avoid odd edge cases involving method
Brett Cannon8045d972011-02-03 22:01:54 +0000322resolution order, etc. This continues to be totally valid in Python 3 (although
323unneeded as all classes implicitly inherit from ``object``).
324
325
326Deal With the Bytes/String Dichotomy
327''''''''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000328
Brett Cannon8045d972011-02-03 22:01:54 +0000329One of the biggest issues people have when porting code to Python 3 is handling
330the bytes/string dichotomy. Because Python 2 allowed the ``str`` type to hold
331textual data, people have over the years been rather loose in their delineation
332of what ``str`` instances held text compared to bytes. In Python 3 you cannot
Brett Cannon9ca21b32014-01-07 11:52:04 -0500333be so care-free anymore and need to properly handle the difference. The key to
R David Murray790e0052012-04-23 14:44:00 -0400334handling this issue is to make sure that **every** string literal in your
Brett Cannon9ca21b32014-01-07 11:52:04 -0500335Python 2 code is either syntactically or functionally marked as either bytes or
Brett Cannon8045d972011-02-03 22:01:54 +0000336text data. After this is done you then need to make sure your APIs are designed
337to either handle a specific type or made to be properly polymorphic.
338
339
340Mark Up Python 2 String Literals
341********************************
342
343First thing you must do is designate every single string literal in Python 2
344as either textual or bytes data. If you are only supporting Python 2.6 or
345newer, this can be accomplished by marking bytes literals with a ``b`` prefix
346and then designating textual data with a ``u`` prefix or using the
347``unicode_literals`` future statement.
348
R David Murray790e0052012-04-23 14:44:00 -0400349If your project supports versions of Python predating 2.6, then you should use
Brett Cannon8045d972011-02-03 22:01:54 +0000350the six_ project and its ``b()`` function to denote bytes literals. For text
351literals you can either use six's ``u()`` function or use a ``u`` prefix.
352
353
354Decide what APIs Will Accept
355****************************
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000356
Brett Cannon8045d972011-02-03 22:01:54 +0000357In Python 2 it was very easy to accidentally create an API that accepted both
358bytes and textual data. But in Python 3, thanks to the more strict handling of
359disparate types, this loose usage of bytes and text together tends to fail.
360
361Take the dict ``{b'a': 'bytes', u'a': 'text'}`` in Python 2.6. It creates the
362dict ``{u'a': 'text'}`` since ``b'a' == u'a'``. But in Python 3 the equivalent
363dict creates ``{b'a': 'bytes', 'a': 'text'}``, i.e., no lost data. Similar
364issues can crop up when transitioning Python 2 code to Python 3.
365
366This means you need to choose what an API is going to accept and create and
367consistently stick to that API in both Python 2 and 3.
368
369
Brett Cannonce71ab22011-02-05 22:05:05 +0000370Bytes / Unicode Comparison
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000371**************************
372
373In Python 3, mixing bytes and unicode is forbidden in most situations; it
374will raise a :class:`TypeError` where Python 2 would have attempted an implicit
375coercion between types. However, there is one case where it doesn't and
376it can be very misleading::
377
378 >>> b"" == ""
379 False
380
Brett Cannona2f15442011-02-09 22:55:13 +0000381This is because an equality comparison is required by the language to always
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000382succeed (and return ``False`` for incompatible types). However, this also
383means that code incorrectly ported to Python 3 can display buggy behaviour
384if such comparisons are silently executed. To detect such situations,
385Python 3 has a ``-b`` flag that will display a warning::
386
387 $ python3 -b
388 >>> b"" == ""
389 __main__:1: BytesWarning: Comparison between bytes and string
390 False
391
392To turn the warning into an exception, use the ``-bb`` flag instead::
393
394 $ python3 -bb
395 >>> b"" == ""
396 Traceback (most recent call last):
397 File "<stdin>", line 1, in <module>
398 BytesWarning: Comparison between bytes and string
399
400
Antoine Pitroubd866e92011-02-05 12:13:38 +0000401Indexing bytes objects
402''''''''''''''''''''''
403
404Another potentially surprising change is the indexing behaviour of bytes
405objects in Python 3::
406
407 >>> b"xyz"[0]
408 120
409
410Indeed, Python 3 bytes objects (as well as :class:`bytearray` objects)
411are sequences of integers. But code converted from Python 2 will often
412assume that indexing a bytestring produces another bytestring, not an
413integer. To reconcile both behaviours, use slicing::
414
415 >>> b"xyz"[0:1]
416 b'x'
417 >>> n = 1
418 >>> b"xyz"[n:n+1]
419 b'y'
420
421The only remaining gotcha is that an out-of-bounds slice returns an empty
422bytes object instead of raising ``IndexError``:
423
424 >>> b"xyz"[3]
425 Traceback (most recent call last):
426 File "<stdin>", line 1, in <module>
427 IndexError: index out of range
428 >>> b"xyz"[3:4]
429 b''
430
431
Brett Cannon8045d972011-02-03 22:01:54 +0000432``__str__()``/``__unicode__()``
433'''''''''''''''''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000434
Brett Cannon8045d972011-02-03 22:01:54 +0000435In Python 2, objects can specify both a string and unicode representation of
436themselves. In Python 3, though, there is only a string representation. This
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000437becomes an issue as people can inadvertently do things in their ``__str__()``
Brett Cannon8045d972011-02-03 22:01:54 +0000438methods which have unpredictable results (e.g., infinite recursion if you
439happen to use the ``unicode(self).encode('utf8')`` idiom as the body of your
440``__str__()`` method).
441
Brett Cannon9ca21b32014-01-07 11:52:04 -0500442You can use a mixin class to work around this. This allows you to only define a
Brett Cannon8045d972011-02-03 22:01:54 +0000443``__unicode__()`` method for your class and let the mixin derive
444``__str__()`` for you (code from
445http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/)::
446
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000447 import sys
Brett Cannon8045d972011-02-03 22:01:54 +0000448
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000449 class UnicodeMixin(object):
Brett Cannon8045d972011-02-03 22:01:54 +0000450
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000451 """Mixin class to handle defining the proper __str__/__unicode__
452 methods in Python 2 or 3."""
Brett Cannon8045d972011-02-03 22:01:54 +0000453
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000454 if sys.version_info[0] >= 3: # Python 3
455 def __str__(self):
456 return self.__unicode__()
457 else: # Python 2
458 def __str__(self):
459 return self.__unicode__().encode('utf8')
Brett Cannon8045d972011-02-03 22:01:54 +0000460
461
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000462 class Spam(UnicodeMixin):
Brett Cannon8045d972011-02-03 22:01:54 +0000463
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000464 def __unicode__(self):
465 return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
Brett Cannon8045d972011-02-03 22:01:54 +0000466
467
Brett Cannon8045d972011-02-03 22:01:54 +0000468Don't Index on Exceptions
469'''''''''''''''''''''''''
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000470
Brett Cannon8045d972011-02-03 22:01:54 +0000471In Python 2, the following worked::
472
Brett Cannon4b0c24a2011-02-03 22:14:58 +0000473 >>> exc = Exception(1, 2, 3)
474 >>> exc.args[1]
475 2
476 >>> exc[1] # Python 2 only!
477 2
Brett Cannon8045d972011-02-03 22:01:54 +0000478
Eli Bendersky7ac34192011-02-07 04:44:19 +0000479But in Python 3, indexing directly on an exception is an error. You need to
480make sure to only index on the :attr:`BaseException.args` attribute which is a
Brett Cannon8045d972011-02-03 22:01:54 +0000481sequence containing all arguments passed to the :meth:`__init__` method.
482
Eli Bendersky7ac34192011-02-07 04:44:19 +0000483Even better is to use the documented attributes the exception provides.
Brett Cannon8045d972011-02-03 22:01:54 +0000484
Brett Cannon9ca21b32014-01-07 11:52:04 -0500485
Brett Cannon8045d972011-02-03 22:01:54 +0000486Don't use ``__getslice__`` & Friends
487''''''''''''''''''''''''''''''''''''
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000488
Brett Cannon8045d972011-02-03 22:01:54 +0000489Been deprecated for a while, but Python 3 finally drops support for
490``__getslice__()``, etc. Move completely over to :meth:`__getitem__` and
491friends.
492
493
Brett Cannon45aa7cc2011-02-05 22:16:40 +0000494Updating doctests
495'''''''''''''''''
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000496
Brett Cannon9ca21b32014-01-07 11:52:04 -0500497Don't forget to make them Python 2/3 compatible as well. If you wrote a
498monolithic set of doctests (e.g., a single docstring containing all of your
499doctests), you should at least consider breaking the doctests up into smaller
500pieces to make it more manageable to fix. Otherwise it might very well be worth
501your time and effort to port your tests to :mod:`unittest`.
Brett Cannon8045d972011-02-03 22:01:54 +0000502
503
Brett Cannon9ca21b32014-01-07 11:52:04 -0500504Update ``map`` for imbalanced input sequences
505'''''''''''''''''''''''''''''''''''''''''''''
Jason R. Coombsa90e3642011-12-03 08:24:21 -0500506
Brett Cannon9ca21b32014-01-07 11:52:04 -0500507With Python 2, ``map`` would pad input sequences of unequal length with
Jason R. Coombsa90e3642011-12-03 08:24:21 -0500508`None` values, returning a sequence as long as the longest input sequence.
509
Brett Cannon9ca21b32014-01-07 11:52:04 -0500510With Python 3, if the input sequences to ``map`` are of unequal length, ``map``
Jason R. Coombsa90e3642011-12-03 08:24:21 -0500511will stop at the termination of the shortest of the sequences. For full
Brett Cannon9ca21b32014-01-07 11:52:04 -0500512compatibility with ``map`` from Python 2.x, also wrap the sequences in
Jason R. Coombsa90e3642011-12-03 08:24:21 -0500513:func:`itertools.zip_longest`, e.g. ``map(func, *sequences)`` becomes
514``list(map(func, itertools.zip_longest(*sequences)))``.
515
Brett Cannon8045d972011-02-03 22:01:54 +0000516Eliminate ``-3`` Warnings
517-------------------------
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000518
Brett Cannon8045d972011-02-03 22:01:54 +0000519When you run your application's test suite, run it using the ``-3`` flag passed
520to Python. This will cause various warnings to be raised during execution about
521things that 2to3 cannot handle automatically (e.g., modules that have been
522removed). Try to eliminate those warnings to make your code even more portable
523to Python 3.
524
525
Brett Cannon9ca21b32014-01-07 11:52:04 -0500526Alternative Approaches
527======================
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000528
Brett Cannon9ca21b32014-01-07 11:52:04 -0500529While supporting Python 2 & 3 simultaneously is typically the preferred choice
530by people so that they can continue to improve code and have it work for the
531most number of users, your life may be easier if you only have to support one
532major version of Python going forward.
533
534Supporting Only Python 3 Going Forward From Python 2 Code
535---------------------------------------------------------
536
537If you have Python 2 code but going forward only want to improve it as Python 3
538code, then you can use 2to3_ to translate your Python 2 code to Python 3 code.
539This is only recommended, though, if your current version of your project is
540going into maintenance mode and you want all new features to be exclusive to
541Python 3.
Brett Cannon8045d972011-02-03 22:01:54 +0000542
543
Brett Cannon9ca21b32014-01-07 11:52:04 -0500544Backporting Python 3 code to Python 2
545-------------------------------------
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000546
Brett Cannon9ca21b32014-01-07 11:52:04 -0500547If you have Python 3 code and have little interest in supporting Python 2 you
548can use 3to2_ to translate from Python 3 code to Python 2 code. This is only
549recommended if you don't plan to heavily support Python 2 users.
Brett Cannon8045d972011-02-03 22:01:54 +0000550
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000551
Brett Cannon8045d972011-02-03 22:01:54 +0000552Other Resources
553===============
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000554
Brett Cannon6277fa42011-02-18 01:34:28 +0000555The authors of the following blog posts, wiki pages, and books deserve special
556thanks for making public their tips for porting Python 2 code to Python 3 (and
Brett Cannon9ca21b32014-01-07 11:52:04 -0500557thus helping provide information for this document and its various revisions
558over the years):
Brett Cannon8045d972011-02-03 22:01:54 +0000559
Brett Cannon9ca21b32014-01-07 11:52:04 -0500560* http://wiki.python.org/moin/PortingPythonToPy3k
Brett Cannon6277fa42011-02-18 01:34:28 +0000561* http://python3porting.com/
Brett Cannon8045d972011-02-03 22:01:54 +0000562* http://docs.pythonsprints.com/python3_porting/py-porting.html
563* http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/
564* http://dabeaz.blogspot.com/2011/01/porting-py65-and-my-superboard-to.html
565* http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/
566* http://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide/
Brett Cannonb7e6b892013-03-09 14:22:35 -0500567* https://wiki.ubuntu.com/Python/3
Brett Cannon8045d972011-02-03 22:01:54 +0000568
569If you feel there is something missing from this document that should be added,
570please email the python-porting_ mailing list.
571
Brett Cannon9ca21b32014-01-07 11:52:04 -0500572
573
574.. _2to3: http://docs.python.org/2/library/2to3.html
575.. _3to2: https://pypi.python.org/pypi/3to2
576.. _Cheeseshop: PyPI_
577.. _coverage: https://pypi.python.org/pypi/coverage
578.. _future: http://python-future.org/
579.. _modernize: https://github.com/mitsuhiko/python-modernize
580.. _Porting to Python 3: http://python3porting.com/
581.. _PyPI: http://pypi.python.org/
582.. _Python 2.2: http://www.python.org/2.2.x
583.. _Python 2.5: http://www.python.org/2.5.x
584.. _Python 2.6: http://www.python.org/2.6.x
585.. _Python 2.7: http://www.python.org/2.7.x
586.. _Python 2.5: http://www.python.org/2.5.x
587.. _Python 3.3: http://www.python.org/3.3.x
588.. _Python 3 Packages: https://pypi.python.org/pypi?:action=browse&c=533&show=all
589.. _Python 3 Q & A: http://ncoghlan-devs-python-notes.readthedocs.org/en/latest/python3/questions_and_answers.html
Brett Cannon8045d972011-02-03 22:01:54 +0000590.. _python-porting: http://mail.python.org/mailman/listinfo/python-porting
Brett Cannon9ca21b32014-01-07 11:52:04 -0500591.. _six: https://pypi.python.org/pypi/six
592.. _tox: https://pypi.python.org/pypi/tox
593.. _trove classifiers: https://pypi.python.org/pypi?%3Aaction=list_classifiers
594