|
30 | 30 |
|
31 | 31 | #. Update your code to drop support for Python 2.5 or older (supporting only |
32 | 32 | Python 2.7 is ideal) |
33 | | -#. Make sure you have good test coverage (coverage.py_ can help) |
| 33 | +#. Make sure you have good test coverage (coverage.py_ can help; |
| 34 | + ``pip install coverage``) |
34 | 35 | #. Learn the differences between Python 2 & 3 |
35 | | -#. Use Modernize_ or Futurize_ to update your code |
| 36 | +#. Use Modernize_ or Futurize_ to update your code (``pip install modernize`` or |
| 37 | + ``pip install future``, respectively) |
36 | 38 | #. Use Pylint_ to help make sure you don't regress on your Python 3 support |
37 | | - (if only supporting Python 2.7/3.4 or newer) |
| 39 | + (if only supporting Python 2.7/3.4 or newer; ``pip install pylint``) |
38 | 40 | #. Use caniusepython3_ to find out which of your dependencies are blocking your |
39 | | - use of Python 3 |
| 41 | + use of Python 3 (``pip install caniusepython3``) |
40 | 42 | #. Once your dependencies are no longer blocking you, use continuous integration |
41 | 43 | to make sure you stay compatible with Python 2 & 3 (tox_ can help test |
42 | | - against multiple versions of Python) |
| 44 | + against multiple versions of Python; ``pip install tox``) |
43 | 45 |
|
44 | 46 | If you are dropping support for Python 2 entirely, then after you learn the |
45 | 47 | differences between Python 2 & 3 you can run 2to3_ over your code and skip the |
@@ -71,8 +73,9 @@ Drop support for Python 2.5 and older (at least) |
71 | 73 | While you can make Python 2.5 work with Python 3, it is **much** easier if you |
72 | 74 | only have to work with Python 2.6 or newer (and easier still if you only have |
73 | 75 | to work with Python 2.7). If dropping Python 2.5 is not an option then the six_ |
74 | | -project can help you support Python 2.5 & 3 simultaneously. Do realize, though, |
75 | | -that nearly all the projects listed in this HOWTO will not be available to you. |
| 76 | +project can help you support Python 2.5 & 3 simultaneously |
| 77 | +(``pip install six``). Do realize, though, that nearly all the projects listed |
| 78 | +in this HOWTO will not be available to you. |
76 | 79 |
|
77 | 80 | If you are able to only support Python 2.6 or newer, then the required changes |
78 | 81 | to your code should continue to look and feel like idiomatic Python code. At |
@@ -115,7 +118,8 @@ Python 3! But to fully understand how your code is going to change and what |
115 | 118 | you want to look out for while you code, you will want to learn what changes |
116 | 119 | Python 3 makes in terms of Python 2. Typically the two best ways of doing that |
117 | 120 | is reading the `"What's New"`_ doc for each release of Python 3 and the |
118 | | -`Porting to Python 3`_ book (which is free online). |
| 121 | +`Porting to Python 3`_ book (which is free online). There is also a handy |
| 122 | +`cheat sheet`_ from the Python-Future project. |
119 | 123 |
|
120 | 124 |
|
121 | 125 | Update your code |
@@ -245,6 +249,16 @@ for opening files instead of the built-in :func:`open` function as the :mod:`io` |
245 | 249 | module is consistent from Python 2 to 3 while the built-in :func:`open` function |
246 | 250 | is not (in Python 3 it's actually :func:`io.open`). |
247 | 251 |
|
| 252 | +The constructors of both ``str`` and ``bytes`` have different semantics for the |
| 253 | +same arguments between Python 2 & 3. Passing an integer to ``bytes`` in Python 2 |
| 254 | +will give you the string representation of the integer: ``bytes(3) == '3'``. |
| 255 | +But in Python 3, an integer argument to ``bytes`` will give you a bytes object |
| 256 | +as long as the integer specified, filled with null bytes: |
| 257 | +``bytes(3) == b'\x00\x00\x00'``. A similar worry is necessary when passing a |
| 258 | +bytes 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 |
| 260 | +bytes object: ``str(b'3') == "b'3'"``. |
| 261 | + |
248 | 262 | Finally, the indexing of binary data requires careful handling (slicing does |
249 | 263 | **not** require any special handling). In Python 2, |
250 | 264 | ``b'123'[1] == b'2'`` while in Python 3 ``b'123'[1] == 50``. Because binary data |
@@ -362,6 +376,7 @@ supported by Python 2. You should also update the classifiers in your |
362 | 376 |
|
363 | 377 | .. _2to3: https://docs.python.org/3/library/2to3.html |
364 | 378 | .. _caniusepython3: https://pypi.python.org/pypi/caniusepython3 |
| 379 | +.. _cheat sheet: http://python-future.org/compatible_idioms.html |
365 | 380 | .. _coverage.py: https://pypi.python.org/pypi/coverage |
366 | 381 | .. _Futurize: http://python-future.org/automatic_conversion.html |
367 | 382 | .. _Modernize: |
|
0 commit comments