blob: c479f2264e95ecd701fcade91b8072f21bfec6d5 [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 For help with porting, you can email the python-porting_ mailing list with
23 questions.
Brett Cannon8045d972011-02-03 22:01:54 +000024
Brett Cannon6b335192014-12-05 10:56:12 -050025The Short Explanation
26=====================
Larry Hastings3732ed22014-03-15 21:13:56 -070027
Brett Cannon6b335192014-12-05 10:56:12 -050028To make your project be single-source Python 2/3 compatible, the basic steps
29are:
Larry Hastings3732ed22014-03-15 21:13:56 -070030
Brett Cannon2645bad2015-03-13 12:49:44 -040031#. Only worry about supporting Python 2.7
Brett Cannon90783eb2014-12-12 15:13:43 -050032#. Make sure you have good test coverage (coverage.py_ can help;
33 ``pip install coverage``)
Brett Cannon6b335192014-12-05 10:56:12 -050034#. Learn the differences between Python 2 & 3
Brett Cannon90783eb2014-12-12 15:13:43 -050035#. Use Modernize_ or Futurize_ to update your code (``pip install modernize`` or
36 ``pip install future``, respectively)
Brett Cannon6b335192014-12-05 10:56:12 -050037#. Use Pylint_ to help make sure you don't regress on your Python 3 support
Brett Cannon2645bad2015-03-13 12:49:44 -040038 (``pip install pylint``)
Brett Cannon6b335192014-12-05 10:56:12 -050039#. Use caniusepython3_ to find out which of your dependencies are blocking your
Brett Cannon90783eb2014-12-12 15:13:43 -050040 use of Python 3 (``pip install caniusepython3``)
Brett Cannon6b335192014-12-05 10:56:12 -050041#. Once your dependencies are no longer blocking you, use continuous integration
42 to make sure you stay compatible with Python 2 & 3 (tox_ can help test
Brett Cannon90783eb2014-12-12 15:13:43 -050043 against multiple versions of Python; ``pip install tox``)
Larry Hastings3732ed22014-03-15 21:13:56 -070044
Brett Cannon6b335192014-12-05 10:56:12 -050045If you are dropping support for Python 2 entirely, then after you learn the
46differences between Python 2 & 3 you can run 2to3_ over your code and skip the
47rest of the steps outlined above.
Georg Brandl728e4de2014-10-29 09:00:30 +010048
Larry Hastings3732ed22014-03-15 21:13:56 -070049
Brett Cannon6b335192014-12-05 10:56:12 -050050Details
51=======
Larry Hastings3732ed22014-03-15 21:13:56 -070052
Brett Cannon6b335192014-12-05 10:56:12 -050053A key point about supporting Python 2 & 3 simultaneously is that you can start
54**today**! Even if your dependencies are not supporting Python 3 yet that does
55not mean you can't modernize your code **now** to support Python 3. Most changes
56required to support Python 3 lead to cleaner code using newer practices even in
57Python 2.
Larry Hastings3732ed22014-03-15 21:13:56 -070058
Brett Cannon6b335192014-12-05 10:56:12 -050059Another key point is that modernizing your Python 2 code to also support
60Python 3 is largely automated for you. While you might have to make some API
61decisions thanks to Python 3 clarifying text data versus binary data, the
62lower-level work is now mostly done for you and thus can at least benefit from
63the automated changes immediately.
Brett Cannon8045d972011-02-03 22:01:54 +000064
Brett Cannon6b335192014-12-05 10:56:12 -050065Keep those key points in mind while you read on about the details of porting
66your code to support Python 2 & 3 simultaneously.
Georg Brandl2cb2fa92011-02-07 15:30:45 +000067
Brett Cannon8045d972011-02-03 22:01:54 +000068
Brett Cannon2645bad2015-03-13 12:49:44 -040069Drop support for Python 2.6 and older
70-------------------------------------
Brett Cannon8045d972011-02-03 22:01:54 +000071
Brett Cannon6b335192014-12-05 10:56:12 -050072While you can make Python 2.5 work with Python 3, it is **much** easier if you
Brett Cannon2645bad2015-03-13 12:49:44 -040073only have to work with Python 2.7. If dropping Python 2.5 is not an
74option then the six_ project can help you support Python 2.5 & 3 simultaneously
Brett Cannon90783eb2014-12-12 15:13:43 -050075(``pip install six``). Do realize, though, that nearly all the projects listed
76in this HOWTO will not be available to you.
Brett Cannon8045d972011-02-03 22:01:54 +000077
Brett Cannon2645bad2015-03-13 12:49:44 -040078If you are able to skip Python 2.5 and older, then the required changes
Brett Cannon6b335192014-12-05 10:56:12 -050079to your code should continue to look and feel like idiomatic Python code. At
80worst you will have to use a function instead of a method in some instances or
81have to import a function instead of using a built-in one, but otherwise the
82overall transformation should not feel foreign to you.
Brett Cannon8045d972011-02-03 22:01:54 +000083
Brett Cannon2645bad2015-03-13 12:49:44 -040084But you should aim for only supporting Python 2.7. Python 2.6 is no longer
85supported and thus is not receiving bugfixes. This means **you** will have to
86work around any issues you come across with Python 2.6. There are also some
87tools mentioned in this HOWTO which do not support Python 2.6 (e.g., Pylint_),
88and this will become more commonplace as time goes on. It will simply be easier
89for you if you only support the versions of Python that you have to support.
Brett Cannon8045d972011-02-03 22:01:54 +000090
Brett Cannon6b335192014-12-05 10:56:12 -050091Make sure you specify the proper version support in your ``setup.py`` file
92--------------------------------------------------------------------------
Brett Cannonb7e6b892013-03-09 14:22:35 -050093
Brett Cannon6b335192014-12-05 10:56:12 -050094In your ``setup.py`` file you should have the proper `trove classifier`_
95specifying what versions of Python you support. As your project does not support
96Python 3 yet you should at least have
97``Programming Language :: Python :: 2 :: Only`` specified. Ideally you should
98also specify each major/minor version of Python that you do support, e.g.
99``Programming Language :: Python :: 2.7``.
Brett Cannon8045d972011-02-03 22:01:54 +0000100
Brett Cannon6b335192014-12-05 10:56:12 -0500101Have good test coverage
102-----------------------
Brett Cannon9ca21b32014-01-07 11:52:04 -0500103
Brett Cannon6b335192014-12-05 10:56:12 -0500104Once you have your code supporting the oldest version of Python 2 you want it
105to, you will want to make sure your test suite has good coverage. A good rule of
106thumb is that if you want to be confident enough in your test suite that any
107failures that appear after having tools rewrite your code are actual bugs in the
108tools and not in your code. If you want a number to aim for, try to get over 80%
109coverage (and don't feel bad if you can't easily get past 90%). If you
110don't already have a tool to measure test coverage then coverage.py_ is
111recommended.
Brett Cannon9ca21b32014-01-07 11:52:04 -0500112
Brett Cannon6b335192014-12-05 10:56:12 -0500113Learn the differences between Python 2 & 3
114-------------------------------------------
Brett Cannon9ca21b32014-01-07 11:52:04 -0500115
Brett Cannon6b335192014-12-05 10:56:12 -0500116Once you have your code well-tested you are ready to begin porting your code to
117Python 3! But to fully understand how your code is going to change and what
118you want to look out for while you code, you will want to learn what changes
119Python 3 makes in terms of Python 2. Typically the two best ways of doing that
120is reading the `"What's New"`_ doc for each release of Python 3 and the
Brett Cannon90783eb2014-12-12 15:13:43 -0500121`Porting to Python 3`_ book (which is free online). There is also a handy
122`cheat sheet`_ from the Python-Future project.
Brett Cannon9ca21b32014-01-07 11:52:04 -0500123
Brett Cannon9ca21b32014-01-07 11:52:04 -0500124
Brett Cannon6b335192014-12-05 10:56:12 -0500125Update your code
126----------------
Brett Cannon8045d972011-02-03 22:01:54 +0000127
Brett Cannon6b335192014-12-05 10:56:12 -0500128Once you feel like you know what is different in Python 3 compared to Python 2,
129it's time to update your code! You have a choice between two tools in porting
130your code automatically: Modernize_ and Futurize_. Which tool you choose will
131depend on how much like Python 3 you want your code to be. Futurize_ does its
132best to make Python 3 idioms and practices exist in Python 2, e.g. backporting
133the ``bytes`` type from Python 3 so that you have semantic parity between the
134major versions of Python. Modernize_,
135on the other hand, is more conservative and targets a Python 2/3 subset of
136Python, relying on six_ to help provide compatibility.
Brett Cannon8045d972011-02-03 22:01:54 +0000137
Brett Cannon6b335192014-12-05 10:56:12 -0500138Regardless of which tool you choose, they will update your code to run under
139Python 3 while staying compatible with the version of Python 2 you started with.
140Depending on how conservative you want to be, you may want to run the tool over
141your test suite first and visually inspect the diff to make sure the
142transformation is accurate. After you have transformed your test suite and
143verified that all the tests still pass as expected, then you can transform your
144application code knowing that any tests which fail is a translation failure.
Brett Cannon8045d972011-02-03 22:01:54 +0000145
Brett Cannon6b335192014-12-05 10:56:12 -0500146Unfortunately the tools can't automate everything to make your code work under
147Python 3 and so there are a handful of things you will need to update manually
148to get full Python 3 support (which of these steps are necessary vary between
149the tools). Read the documentation for the tool you choose to use to see what it
150fixes by default and what it can do optionally to know what will (not) be fixed
151for you and what you may have to fix on your own (e.g. using ``io.open()`` over
152the built-in ``open()`` function is off by default in Modernize). Luckily,
153though, there are only a couple of things to watch out for which can be
154considered large issues that may be hard to debug if not watched for.
Brett Cannon8045d972011-02-03 22:01:54 +0000155
Brett Cannon6b335192014-12-05 10:56:12 -0500156Division
157++++++++
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000158
Brett Cannon6b335192014-12-05 10:56:12 -0500159In Python 3, ``5 / 2 == 2.5`` and not ``2``; all division between ``int`` values
160result in a ``float``. This change has actually been planned since Python 2.2
161which was released in 2002. Since then users have been encouraged to add
162``from __future__ import division`` to any and all files which use the ``/`` and
163``//`` operators or to be running the interpreter with the ``-Q`` flag. If you
164have not been doing this then you will need to go through your code and do two
165things:
Brett Cannon8045d972011-02-03 22:01:54 +0000166
Brett Cannon6b335192014-12-05 10:56:12 -0500167#. Add ``from __future__ import division`` to your files
168#. Update any division operator as necessary to either use ``//`` to use floor
169 division or continue using ``/`` and expect a float
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000170
Brett Cannon6b335192014-12-05 10:56:12 -0500171The reason that ``/`` isn't simply translated to ``//`` automatically is that if
Brett Cannonfdde79d2015-02-27 15:10:03 -0500172an object defines a ``__truediv__`` method but not ``__floordiv__`` then your
173code would begin to fail (e.g. a user-defined class that uses ``/`` to
174signify some operation but not ``//`` for the same thing or at all).
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000175
Brett Cannon6b335192014-12-05 10:56:12 -0500176Text versus binary data
177+++++++++++++++++++++++
Brett Cannon8045d972011-02-03 22:01:54 +0000178
Brett Cannon6b335192014-12-05 10:56:12 -0500179In Python 2 you could use the ``str`` type for both text and binary data.
180Unfortunately this confluence of two different concepts could lead to brittle
181code which sometimes worked for either kind of data, sometimes not. It also
182could lead to confusing APIs if people didn't explicitly state that something
183that accepted ``str`` accepted either text or binary data instead of one
184specific type. This complicated the situation especially for anyone supporting
185multiple languages as APIs wouldn't bother explicitly supporting ``unicode``
186when they claimed text data support.
Brett Cannon8045d972011-02-03 22:01:54 +0000187
Brett Cannon6b335192014-12-05 10:56:12 -0500188To make the distinction between text and binary data clearer and more
189pronounced, Python 3 did what most languages created in the age of the internet
190have done and made text and binary data distinct types that cannot blindly be
191mixed together (Python predates widespread access to the internet). For any code
192that only deals with text or only binary data, this separation doesn't pose an
193issue. But for code that has to deal with both, it does mean you might have to
194now care about when you are using text compared to binary data, which is why
195this cannot be entirely automated.
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000196
Brett Cannon6b335192014-12-05 10:56:12 -0500197To start, you will need to decide which APIs take text and which take binary
198(it is **highly** recommended you don't design APIs that can take both due to
199the difficulty of keeping the code working; as stated earlier it is difficult to
200do well). In Python 2 this means making sure the APIs that take text can work
201with ``unicode`` in Python 2 and those that work with binary data work with the
202``bytes`` type from Python 3 and thus a subset of ``str`` in Python 2 (which the
203``bytes`` type in Python 2 is an alias for). Usually the biggest issue is
204realizing which methods exist for which types in Python 2 & 3 simultaneously
205(for text that's ``unicode`` in Python 2 and ``str`` in Python 3, for binary
206that's ``str``/``bytes`` in Python 2 and ``bytes`` in Python 3). The following
207table lists the **unique** methods of each data type across Python 2 & 3
208(e.g., the ``decode()`` method is usable on the equivalent binary data type in
209either Python 2 or 3, but it can't be used by the text data type consistently
Brett Cannon8396b8e2015-04-13 16:32:16 -0400210between Python 2 and 3 because ``str`` in Python 3 doesn't have the method). Do
Brett Cannonfd53f982015-04-13 16:21:07 -0400211note that as of Python 3.5 the ``__mod__`` method was added to the bytes type.
Brett Cannonce71ab22011-02-05 22:05:05 +0000212
Brett Cannon6b335192014-12-05 10:56:12 -0500213======================== =====================
214**Text data** **Binary data**
215------------------------ ---------------------
Brett Cannon6b335192014-12-05 10:56:12 -0500216\ decode
217------------------------ ---------------------
218encode
219------------------------ ---------------------
220format
221------------------------ ---------------------
222isdecimal
223------------------------ ---------------------
224isnumeric
225======================== =====================
Brett Cannonce71ab22011-02-05 22:05:05 +0000226
Brett Cannon6b335192014-12-05 10:56:12 -0500227Making the distinction easier to handle can be accomplished by encoding and
228decoding between binary data and text at the edge of your code. This means that
229when you receive text in binary data, you should immediately decode it. And if
230your code needs to send text as binary data then encode it as late as possible.
231This allows your code to work with only text internally and thus eliminates
232having to keep track of what type of data you are working with.
Brett Cannonce71ab22011-02-05 22:05:05 +0000233
Brett Cannon6b335192014-12-05 10:56:12 -0500234The next issue is making sure you know whether the string literals in your code
235represent text or binary data. At minimum you should add a ``b`` prefix to any
236literal that presents binary data. For text you should either use the
237``from __future__ import unicode_literals`` statement or add a ``u`` prefix to
238the text literal.
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000239
Brett Cannon6b335192014-12-05 10:56:12 -0500240As part of this dichotomy you also need to be careful about opening files.
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000241Unless you have been working on Windows, there is a chance you have not always
242bothered to add the ``b`` mode when opening a binary file (e.g., ``rb`` for
243binary reading). Under Python 3, binary files and text files are clearly
244distinct and mutually incompatible; see the :mod:`io` module for details.
245Therefore, you **must** make a decision of whether a file will be used for
Martin Panterc04fb562016-02-10 05:44:01 +0000246binary access (allowing binary data to be read and/or written) or text access
247(allowing text data to be read and/or written). You should also use :func:`io.open`
Brett Cannon6b335192014-12-05 10:56:12 -0500248for opening files instead of the built-in :func:`open` function as the :mod:`io`
249module is consistent from Python 2 to 3 while the built-in :func:`open` function
250is not (in Python 3 it's actually :func:`io.open`).
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000251
Brett Cannon90783eb2014-12-12 15:13:43 -0500252The constructors of both ``str`` and ``bytes`` have different semantics for the
253same arguments between Python 2 & 3. Passing an integer to ``bytes`` in Python 2
254will give you the string representation of the integer: ``bytes(3) == '3'``.
255But in Python 3, an integer argument to ``bytes`` will give you a bytes object
256as long as the integer specified, filled with null bytes:
257``bytes(3) == b'\x00\x00\x00'``. A similar worry is necessary when passing a
258bytes object to ``str``. In Python 2 you just get the bytes object back:
259``str(b'3') == b'3'``. But in Python 3 you get the string representation of the
260bytes object: ``str(b'3') == "b'3'"``.
261
Brett Cannon6b335192014-12-05 10:56:12 -0500262Finally, the indexing of binary data requires careful handling (slicing does
263**not** require any special handling). In Python 2,
264``b'123'[1] == b'2'`` while in Python 3 ``b'123'[1] == 50``. Because binary data
265is simply a collection of binary numbers, Python 3 returns the integer value for
266the byte you index on. But in Python 2 because ``bytes == str``, indexing
267returns a one-item slice of bytes. The six_ project has a function
268named ``six.indexbytes()`` which will return an integer like in Python 3:
269``six.indexbytes(b'123', 1)``.
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000270
Brett Cannon6b335192014-12-05 10:56:12 -0500271To summarize:
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000272
Brett Cannon6b335192014-12-05 10:56:12 -0500273#. Decide which of your APIs take text and which take binary data
274#. Make sure that your code that works with text also works with ``unicode`` and
275 code for binary data works with ``bytes`` in Python 2 (see the table above
276 for what methods you cannot use for each type)
277#. Mark all binary literals with a ``b`` prefix, use a ``u`` prefix or
278 :mod:`__future__` import statement for text literals
279#. Decode binary data to text as soon as possible, encode text as binary data as
280 late as possible
281#. Open files using :func:`io.open` and make sure to specify the ``b`` mode when
282 appropriate
283#. Be careful when indexing binary data
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000284
Brett Cannonadcb6542016-03-18 13:23:58 -0700285
286Use feature detection instead of version detection
287++++++++++++++++++++++++++++++++++++++++++++++++++
288Inevitably you will have code that has to choose what to do based on what
289version of Python is running. The best way to do this is with feature detection
290of whether the version of Python you're running under supports what you need.
291If for some reason that doesn't work then you should make the version check is
292against Python 2 and not Python 3. To help explain this, let's look at an
293example.
294
295Let's pretend that you need access to a feature of importlib_ that
296is available in Python's standard library since Python 3.3 and available for
297Python 2 through importlib2_ on PyPI. You might be tempted to write code to
298access e.g. the ``importlib.abc`` module by doing the following::
299
300 import sys
301
302 if sys.version[0] == 3:
303 from importlib import abc
304 else:
305 from importlib2 import abc
306
307The problem with this code is what happens when Python 4 comes out? It would
308be better to treat Python 2 as the exceptional case instead of Python 3 and
309assume that future Python versions will be more compatible with Python 3 than
310Python 2::
311
312 import sys
313
314 if sys.version[0] > 2:
315 from importlib import abc
316 else:
317 from importlib2 import abc
318
319The best solution, though, is to do no version detection at all and instead rely
320on feature detection. That avoids any potential issues of getting the version
321detection wrong and helps keep you future-compatible::
322
323 try:
324 from importlib import abc
325 except ImportError:
326 from importlib2 import abc
327
328
Brett Cannon6b335192014-12-05 10:56:12 -0500329Prevent compatibility regressions
330---------------------------------
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000331
Brett Cannon6b335192014-12-05 10:56:12 -0500332Once you have fully translated your code to be compatible with Python 3, you
333will want to make sure your code doesn't regress and stop working under
334Python 3. This is especially true if you have a dependency which is blocking you
335from actually running under Python 3 at the moment.
Antoine Pitrou5c28cfdc2011-02-05 11:53:39 +0000336
Brett Cannon6b335192014-12-05 10:56:12 -0500337To help with staying compatible, any new modules you create should have
338at least the following block of code at the top of it::
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000339
Brett Cannon6b335192014-12-05 10:56:12 -0500340 from __future__ import absolute_import
341 from __future__ import division
Berker Peksagbd62f0a2014-12-13 15:48:22 +0200342 from __future__ import print_function
Brett Cannon6b335192014-12-05 10:56:12 -0500343 from __future__ import unicode_literals
Brett Cannon8045d972011-02-03 22:01:54 +0000344
Brett Cannon6b335192014-12-05 10:56:12 -0500345You can also run Python 2 with the ``-3`` flag to be warned about various
346compatibility issues your code triggers during execution. If you turn warnings
347into errors with ``-Werror`` then you can make sure that you don't accidentally
348miss a warning.
Brett Cannon8045d972011-02-03 22:01:54 +0000349
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000350
Brett Cannon6b335192014-12-05 10:56:12 -0500351You can also use the Pylint_ project and its ``--py3k`` flag to lint your code
352to receive warnings when your code begins to deviate from Python 3
353compatibility. This also prevents you from having to run Modernize_ or Futurize_
354over your code regularly to catch compatibility regressions. This does require
355you only support Python 2.7 and Python 3.4 or newer as that is Pylint's
356minimum Python version support.
Brett Cannon8045d972011-02-03 22:01:54 +0000357
358
Brett Cannon6b335192014-12-05 10:56:12 -0500359Check which dependencies block your transition
360----------------------------------------------
Brett Cannon8045d972011-02-03 22:01:54 +0000361
Brett Cannon6b335192014-12-05 10:56:12 -0500362**After** you have made your code compatible with Python 3 you should begin to
363care about whether your dependencies have also been ported. The caniusepython3_
364project was created to help you determine which projects
365-- directly or indirectly -- are blocking you from supporting Python 3. There
366is both a command-line tool as well as a web interface at
367https://caniusepython3.com .
Brett Cannon8045d972011-02-03 22:01:54 +0000368
Brett Cannon6b335192014-12-05 10:56:12 -0500369The project also provides code which you can integrate into your test suite so
370that you will have a failing test when you no longer have dependencies blocking
371you from using Python 3. This allows you to avoid having to manually check your
372dependencies and to be notified quickly when you can start running on Python 3.
Brett Cannon8045d972011-02-03 22:01:54 +0000373
Brett Cannon6b335192014-12-05 10:56:12 -0500374Update your ``setup.py`` file to denote Python 3 compatibility
375--------------------------------------------------------------
Brett Cannon8045d972011-02-03 22:01:54 +0000376
Brett Cannon6b335192014-12-05 10:56:12 -0500377Once your code works under Python 3, you should update the classifiers in
378your ``setup.py`` to contain ``Programming Language :: Python :: 3`` and to not
379specify sole Python 2 support. This will tell
380anyone using your code that you support Python 2 **and** 3. Ideally you will
381also want to add classifiers for each major/minor version of Python you now
382support.
Georg Brandl2cb2fa92011-02-07 15:30:45 +0000383
Brett Cannon6b335192014-12-05 10:56:12 -0500384Use continuous integration to stay compatible
385---------------------------------------------
Brett Cannon8045d972011-02-03 22:01:54 +0000386
Brett Cannon6b335192014-12-05 10:56:12 -0500387Once you are able to fully run under Python 3 you will want to make sure your
388code always works under both Python 2 & 3. Probably the best tool for running
389your tests under multiple Python interpreters is tox_. You can then integrate
390tox with your continuous integration system so that you never accidentally break
391Python 2 or 3 support.
Brett Cannon8045d972011-02-03 22:01:54 +0000392
Martin Pantera90a4a92016-05-30 04:04:50 +0000393You may also want to use the ``-bb`` flag with the Python 3 interpreter to
Brett Cannon4269d6d2015-04-13 14:37:50 -0400394trigger an exception when you are comparing bytes to strings or bytes to an int
395(the latter is available starting in Python 3.5). By default type-differing
Antoine Pitrou3764fc22015-04-13 21:07:57 +0200396comparisons simply return ``False``, but if you made a mistake in your
Brett Cannon4269d6d2015-04-13 14:37:50 -0400397separation of text/binary data handling or indexing on bytes you wouldn't easily
398find the mistake. This flag will raise an exception when these kinds of
399comparisons occur, making the mistake much easier to track down.
Brett Cannon8045d972011-02-03 22:01:54 +0000400
Brett Cannon6b335192014-12-05 10:56:12 -0500401And that's mostly it! At this point your code base is compatible with both
402Python 2 and 3 simultaneously. Your testing will also be set up so that you
403don't accidentally break Python 2 or 3 compatibility regardless of which version
404you typically run your tests under while developing.
Brett Cannon8045d972011-02-03 22:01:54 +0000405
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000406
Brett Cannon6b335192014-12-05 10:56:12 -0500407Dropping Python 2 support completely
408====================================
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000409
Brett Cannon6b335192014-12-05 10:56:12 -0500410If you are able to fully drop support for Python 2, then the steps required
411to transition to Python 3 simplify greatly.
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000412
Brett Cannon6b335192014-12-05 10:56:12 -0500413#. Update your code to only support Python 2.7
414#. Make sure you have good test coverage (coverage.py_ can help)
415#. Learn the differences between Python 2 & 3
416#. Use 2to3_ to rewrite your code to run only under Python 3
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000417
Brett Cannon6b335192014-12-05 10:56:12 -0500418After this your code will be fully Python 3 compliant but in a way that is not
419supported by Python 2. You should also update the classifiers in your
420``setup.py`` to contain ``Programming Language :: Python :: 3 :: Only``.
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000421
Antoine Pitrou8d8f7c52011-02-05 11:40:05 +0000422
Brett Cannon6b335192014-12-05 10:56:12 -0500423.. _2to3: https://docs.python.org/3/library/2to3.html
Brett Cannon17be09c2014-12-05 18:11:05 -0500424.. _caniusepython3: https://pypi.python.org/pypi/caniusepython3
Brett Cannon90783eb2014-12-12 15:13:43 -0500425.. _cheat sheet: http://python-future.org/compatible_idioms.html
Brett Cannon6b335192014-12-05 10:56:12 -0500426.. _coverage.py: https://pypi.python.org/pypi/coverage
427.. _Futurize: http://python-future.org/automatic_conversion.html
Brett Cannonadcb6542016-03-18 13:23:58 -0700428.. _importlib: https://docs.python.org/3/library/importlib.html#module-importlib
429.. _importlib2: https://pypi.python.org/pypi/importlib2
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300430.. _Modernize: https://python-modernize.readthedocs.org/en/latest/
Brett Cannon9ca21b32014-01-07 11:52:04 -0500431.. _Porting to Python 3: http://python3porting.com/
Brett Cannon6b335192014-12-05 10:56:12 -0500432.. _Pylint: https://pypi.python.org/pypi/pylint
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300433.. _Python 3 Q & A: https://ncoghlan-devs-python-notes.readthedocs.org/en/latest/python3/questions_and_answers.html
Brett Cannon6b335192014-12-05 10:56:12 -0500434
435.. _python-future: http://python-future.org/
Georg Brandle73778c2014-10-29 08:36:35 +0100436.. _python-porting: https://mail.python.org/mailman/listinfo/python-porting
Brett Cannon9ca21b32014-01-07 11:52:04 -0500437.. _six: https://pypi.python.org/pypi/six
438.. _tox: https://pypi.python.org/pypi/tox
Brett Cannon6b335192014-12-05 10:56:12 -0500439.. _trove classifier: https://pypi.python.org/pypi?%3Aaction=list_classifiers
440.. _"What's New": https://docs.python.org/3/whatsnew/index.html