Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 1 | .. _pyporting-howto: |
| 2 | |
| 3 | ********************************* |
| 4 | Porting Python 2 Code to Python 3 |
| 5 | ********************************* |
| 6 | |
| 7 | :author: Brett Cannon |
| 8 | |
| 9 | .. topic:: Abstract |
| 10 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 11 | 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 Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 13 | Python. This guide is meant to help you figure out how best to support both |
| 14 | Python 2 & 3 simultaneously. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 15 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 16 | If you are looking to port an extension module instead of pure Python code, |
Éric Araujo | 5405a0b | 2011-02-05 16:03:12 +0000 | [diff] [blame] | 17 | please see :ref:`cporting-howto`. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 18 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 19 | 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 Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 21 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 22 | 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 Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 25 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 26 | For help with porting, you can email the python-porting_ mailing list with |
| 27 | questions. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 28 | |
| 29 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 30 | Before You Begin |
| 31 | ================ |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 32 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 33 | If your project is on the Cheeseshop_/PyPI_, make sure it has the proper |
| 34 | `trove classifiers`_ to signify what versions of Python it **currently** |
| 35 | supports. At minimum you should specify the major version(s), e.g. |
| 36 | ``Programming Language :: Python :: 2`` if your project currently only supports |
| 37 | Python 2. It is preferrable that you be as specific as possible by listing every |
| 38 | major/minor version of Python that you support, e.g. if your project supports |
| 39 | Python 2.6 and 2.7, then you want the classifiers of:: |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 40 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 41 | Programming Language :: Python :: 2 |
| 42 | Programming Language :: Python :: 2.6 |
| 43 | Programming Language :: Python :: 2.7 |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 45 | Once your project supports Python 3 you will want to go back and add the |
| 46 | appropriate classifiers for Python 3 as well. This is important as setting the |
| 47 | ``Programming Language :: Python :: 3`` classifier will lead to your project |
| 48 | being listed under the `Python 3 Packages`_ section of PyPI. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 49 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 50 | Make sure you have a robust test suite. You need to |
| 51 | make sure everything continues to work, just like when you support a new |
| 52 | minor/feature release of Python. This means making sure your test suite is |
| 53 | thorough and is ported properly between Python 2 & 3 (consider using coverage_ |
| 54 | to measure that you have effective test coverage). You will also most likely |
| 55 | want to use something like tox_ to automate testing between all of your |
| 56 | supported versions of Python. You will also want to **port your tests first** so |
| 57 | that you can make sure that you detect breakage during the transition. Tests also |
| 58 | tend to be simpler than the code they are testing so it gives you an idea of how |
| 59 | easy it can be to port code. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 60 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 61 | Drop support for older Python versions if possible. `Python 2.5`_ |
Eli Bendersky | 2d062de | 2011-02-07 04:19:57 +0000 | [diff] [blame] | 62 | introduced a lot of useful syntax and libraries which have become idiomatic |
| 63 | in Python 3. `Python 2.6`_ introduced future statements which makes |
| 64 | compatibility much easier if you are going from Python 2 to 3. |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 65 | `Python 2.7`_ continues the trend in the stdlib. Choose the newest version |
Eli Bendersky | 2d062de | 2011-02-07 04:19:57 +0000 | [diff] [blame] | 66 | of Python which you believe can be your minimum support version |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 67 | and work from there. |
| 68 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 69 | Target the newest version of Python 3 that you can. Beyond just the usual |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 70 | bugfixes, compatibility has continued to improve between Python 2 and 3 as time |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 71 | has passed. E.g. Python 3.3 added back the ``u`` prefix for |
| 72 | strings, making source-compatible Python code easier to write. |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 73 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 74 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 75 | Writing Source-Compatible Python 2/3 Code |
| 76 | ========================================= |
| 77 | |
| 78 | Over the years the Python community has discovered that the easiest way to |
| 79 | support both Python 2 and 3 in parallel is to write Python code that works in |
| 80 | either version. While this might sound counter-intuitive at first, it actually |
| 81 | is not difficult and typically only requires following some select |
| 82 | (non-idiomatic) practices and using some key projects to help make bridging |
| 83 | between Python 2 and 3 easier. |
| 84 | |
| 85 | Projects to Consider |
| 86 | -------------------- |
| 87 | |
| 88 | The lowest level library for suppoting Python 2 & 3 simultaneously is six_. |
| 89 | Reading through its documentation will give you an idea of where exactly the |
| 90 | Python language changed between versions 2 & 3 and thus what you will want the |
| 91 | library to help you continue to support. |
| 92 | |
| 93 | To help automate porting your code over to using six, you can use |
| 94 | modernize_. This project will attempt to rewrite your code to be as modern as |
| 95 | possible while using six to smooth out any differences between Python 2 & 3. |
| 96 | |
| 97 | If you want to write your compatible code to feel more like Python 3 there is |
| 98 | the future_ project. It tries to provide backports of objects from Python 3 so |
| 99 | that 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. |
| 101 | It also provides a translation script like modernize (its translation code is |
| 102 | actually partially based on it) to help start working with a pre-existing code |
| 103 | base. It is also unique in that its translation script will also port Python 3 |
| 104 | code backwards as well as Python 2 code forwards. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 105 | |
| 106 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 107 | Tips & Tricks |
| 108 | ------------- |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 109 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 110 | To help with writing source-compatible code using one of the projects mentioned |
| 111 | in `Projects to Consider`_, consider following the below suggestions. Some of |
| 112 | them are handled by the suggested projects, so if you do use one of them then |
| 113 | read their documentation first to see which suggestions below will taken care of |
| 114 | for you. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 115 | |
| 116 | Support Python 2.7 |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 117 | ////////////////// |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 118 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 119 | As a first step, make sure that your project is compatible with `Python 2.7`_. |
| 120 | This is just good to do as Python 2.7 is the last release of Python 2 and thus |
| 121 | will be used for a rather long time. It also allows for use of the ``-3`` flag |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 122 | to Python to help discover places in your code where compatibility might be an |
| 123 | issue (the ``-3`` flag is in Python 2.6 but Python 2.7 adds more warnings). |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 124 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 125 | Try to Support `Python 2.6`_ and Newer Only |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 126 | /////////////////////////////////////////// |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 127 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 128 | While 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 |
| 130 | additions, etc. exist only in Python 2.6 and later which greatly assist in |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 131 | supporting Python 3. But if you project must keep support for `Python 2.5`_ then |
| 132 | it is still possible to simultaneously support Python 3. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 133 | |
| 134 | Below are the benefits you gain if you only have to support Python 2.6 and |
| 135 | newer. Some of these options are personal choice while others are |
| 136 | **strongly** recommended (the ones that are more for personal choice are |
| 137 | labeled as such). If you continue to support older versions of Python then you |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 138 | at least need to watch out for situations that these solutions fix and handle |
| 139 | them appropriately (which is where library help from e.g. six_ comes in handy). |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 140 | |
| 141 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 142 | ``from __future__ import print_function`` |
| 143 | ''''''''''''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 144 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 145 | It will not only get you used to typing ``print()`` as a function instead of a |
| 146 | statement, but it will also give you the various benefits the function has over |
| 147 | the Python 2 statement (six_ provides a function if you support Python 2.5 or |
| 148 | older). |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 149 | |
| 150 | |
| 151 | ``from __future__ import unicode_literals`` |
| 152 | ''''''''''''''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 153 | |
Brett Cannon | f0996a9 | 2014-01-17 11:45:01 -0500 | [diff] [blame] | 154 | If you choose to use this future statement then all string literals in |
| 155 | Python 2 will be assumed to be Unicode (as is already the case in Python 3). |
| 156 | If you choose not to use this future statement then you should mark all of your |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 157 | text strings with a ``u`` prefix and only support Python 3.3 or newer. But you |
| 158 | are **strongly** advised to do one or the other (six_ provides a function in |
| 159 | case you don't want to use the future statement **and** you want to support |
| 160 | Python 3.2 or older). |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 161 | |
| 162 | |
Brett Cannon | f0996a9 | 2014-01-17 11:45:01 -0500 | [diff] [blame] | 163 | Bytes/string literals |
| 164 | ''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 165 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 166 | This is a **very** important one. Prefix Python 2 strings that |
| 167 | are meant to contain bytes with a ``b`` prefix to very clearly delineate |
| 168 | what is and is not a Python 3 text string (six_ provides a function to use for |
| 169 | Python 2.5 compatibility). |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 170 | |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 171 | This point cannot be stressed enough: make sure you know what all of your string |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 172 | literals in Python 2 are meant to be in Python 3. Any string literal that |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 173 | should be treated as bytes should have the ``b`` prefix. Any string literal |
| 174 | that should be Unicode/text in Python 2 should either have the ``u`` literal |
| 175 | (supported, but ignored, in Python 3.3 and later) or you should have |
| 176 | ``from __future__ import unicode_literals`` at the top of the file. But the key |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 177 | point is you should know how Python 3 will treat every one one of your string |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 178 | literals and you should mark them as appropriate. |
| 179 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 180 | There are some differences between byte literals in Python 2 and those in |
| 181 | Python 3 thanks to the bytes type just being an alias to ``str`` in Python 2. |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 182 | See the `Handle Common "Gotchas"`_ section for what to watch out for. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 183 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 184 | ``from __future__ import absolute_import`` |
| 185 | '''''''''''''''''''''''''''''''''''''''''' |
| 186 | Discussed in more detail below, but you should use this future statement to |
| 187 | prevent yourself from accidentally using implicit relative imports. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 188 | |
| 189 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 190 | Supporting `Python 2.5`_ and Newer Only |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 191 | /////////////////////////////////////// |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 192 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 193 | If you are supporting `Python 2.5`_ and newer there are still some features of |
| 194 | Python that you can utilize. |
| 195 | |
| 196 | |
Ezio Melotti | c17c1f6 | 2011-04-21 14:49:03 +0300 | [diff] [blame] | 197 | ``from __future__ import absolute_import`` |
| 198 | '''''''''''''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 199 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 200 | Implicit relative imports (e.g., importing ``spam.bacon`` from within |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 201 | ``spam.eggs`` with the statement ``import bacon``) do not work in Python 3. |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 202 | This future statement moves away from that and allows the use of explicit |
| 203 | relative imports (e.g., ``from . import bacon``). |
| 204 | |
| 205 | In `Python 2.5`_ you must use |
| 206 | the __future__ statement to get to use explicit relative imports and prevent |
| 207 | implicit ones. In `Python 2.6`_ explicit relative imports are available without |
| 208 | the statement, but you still want the __future__ statement to prevent implicit |
| 209 | relative imports. In `Python 2.7`_ the __future__ statement is not needed. In |
| 210 | other words, unless you are only supporting Python 2.7 or a version earlier |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 211 | than Python 2.5, use this __future__ statement. |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 212 | |
| 213 | |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 214 | Mark all Unicode strings with a ``u`` prefix |
| 215 | ''''''''''''''''''''''''''''''''''''''''''''' |
| 216 | |
| 217 | While Python 2.6 has a ``__future__`` statement to automatically cause Python 2 |
| 218 | to treat all string literals as Unicode, Python 2.5 does not have that shortcut. |
| 219 | This means you should go through and mark all string literals with a ``u`` |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 220 | prefix to turn them explicitly into text strings where appropriate and only |
| 221 | support Python 3.3 or newer. Otherwise use a project like six_ which provides a |
| 222 | function to pass all text string literals through. |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 223 | |
| 224 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 225 | Capturing the Currently Raised Exception |
| 226 | '''''''''''''''''''''''''''''''''''''''' |
| 227 | |
| 228 | In Python 2.5 and earlier the syntax to access the current exception is:: |
| 229 | |
| 230 | try: |
| 231 | raise Exception() |
| 232 | except Exception, exc: |
| 233 | # Current exception is 'exc'. |
| 234 | pass |
| 235 | |
| 236 | This syntax changed in Python 3 (and backported to `Python 2.6`_ and later) |
| 237 | to:: |
| 238 | |
| 239 | try: |
| 240 | raise Exception() |
| 241 | except Exception as exc: |
| 242 | # Current exception is 'exc'. |
| 243 | # In Python 3, 'exc' is restricted to the block; in Python 2.6/2.7 it will "leak". |
| 244 | pass |
| 245 | |
| 246 | Because of this syntax change you must change how you capture the current |
| 247 | exception in Python 2.5 and earlier to:: |
| 248 | |
| 249 | try: |
| 250 | raise Exception() |
| 251 | except Exception: |
| 252 | import sys |
| 253 | exc = sys.exc_info()[1] |
| 254 | # Current exception is 'exc'. |
| 255 | pass |
| 256 | |
| 257 | You can get more information about the raised exception from |
| 258 | :func:`sys.exc_info` than simply the current exception instance, but you most |
| 259 | likely don't need it. |
| 260 | |
| 261 | .. note:: |
| 262 | In Python 3, the traceback is attached to the exception instance |
| 263 | through the ``__traceback__`` attribute. If the instance is saved in |
| 264 | a local variable that persists outside of the ``except`` block, the |
| 265 | traceback will create a reference cycle with the current frame and its |
| 266 | dictionary of local variables. This will delay reclaiming dead |
| 267 | resources until the next cyclic :term:`garbage collection` pass. |
| 268 | |
| 269 | In Python 2, this problem only occurs if you save the traceback itself |
| 270 | (e.g. the third element of the tuple returned by :func:`sys.exc_info`) |
| 271 | in a variable. |
| 272 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 273 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 274 | Handle Common "Gotchas" |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 275 | /////////////////////// |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 276 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 277 | These are things to watch out for no matter what version of Python 2 you are |
| 278 | supporting which are not syntactic considerations. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 279 | |
| 280 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 281 | ``from __future__ import division`` |
| 282 | ''''''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 283 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 284 | While the exact same outcome can be had by using the ``-Qnew`` argument to |
| 285 | Python, using this future statement lifts the requirement that your users use |
| 286 | the flag to get the expected behavior of division in Python 3 |
| 287 | (e.g., ``1/2 == 0.5; 1//2 == 0``). |
| 288 | |
| 289 | |
| 290 | |
Antoine Pitrou | 5c28cfdc | 2011-02-05 11:53:39 +0000 | [diff] [blame] | 291 | Specify when opening a file as binary |
| 292 | ''''''''''''''''''''''''''''''''''''' |
| 293 | |
| 294 | Unless you have been working on Windows, there is a chance you have not always |
| 295 | bothered to add the ``b`` mode when opening a binary file (e.g., ``rb`` for |
| 296 | binary reading). Under Python 3, binary files and text files are clearly |
| 297 | distinct and mutually incompatible; see the :mod:`io` module for details. |
| 298 | Therefore, you **must** make a decision of whether a file will be used for |
| 299 | binary access (allowing to read and/or write bytes data) or text access |
| 300 | (allowing to read and/or write unicode data). |
| 301 | |
| 302 | Text files |
| 303 | '''''''''' |
| 304 | |
| 305 | Text files created using ``open()`` under Python 2 return byte strings, |
| 306 | while under Python 3 they return unicode strings. Depending on your porting |
| 307 | strategy, this can be an issue. |
| 308 | |
| 309 | If you want text files to return unicode strings in Python 2, you have two |
| 310 | possibilities: |
| 311 | |
| 312 | * Under Python 2.6 and higher, use :func:`io.open`. Since :func:`io.open` |
| 313 | is essentially the same function in both Python 2 and Python 3, it will |
| 314 | help iron out any issues that might arise. |
| 315 | |
| 316 | * If pre-2.6 compatibility is needed, then you should use :func:`codecs.open` |
| 317 | instead. This will make sure that you get back unicode strings in Python 2. |
| 318 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 319 | Subclass ``object`` |
| 320 | ''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 321 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 322 | New-style classes have been around since `Python 2.2`_. You need to make sure |
| 323 | you are subclassing from ``object`` to avoid odd edge cases involving method |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 324 | resolution order, etc. This continues to be totally valid in Python 3 (although |
| 325 | unneeded as all classes implicitly inherit from ``object``). |
| 326 | |
| 327 | |
| 328 | Deal With the Bytes/String Dichotomy |
| 329 | '''''''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 330 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 331 | One of the biggest issues people have when porting code to Python 3 is handling |
| 332 | the bytes/string dichotomy. Because Python 2 allowed the ``str`` type to hold |
| 333 | textual data, people have over the years been rather loose in their delineation |
| 334 | of what ``str`` instances held text compared to bytes. In Python 3 you cannot |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 335 | be so care-free anymore and need to properly handle the difference. The key to |
R David Murray | 790e005 | 2012-04-23 14:44:00 -0400 | [diff] [blame] | 336 | handling this issue is to make sure that **every** string literal in your |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 337 | Python 2 code is either syntactically or functionally marked as either bytes or |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 338 | text data. After this is done you then need to make sure your APIs are designed |
| 339 | to either handle a specific type or made to be properly polymorphic. |
| 340 | |
| 341 | |
| 342 | Mark Up Python 2 String Literals |
| 343 | ******************************** |
| 344 | |
| 345 | First thing you must do is designate every single string literal in Python 2 |
| 346 | as either textual or bytes data. If you are only supporting Python 2.6 or |
| 347 | newer, this can be accomplished by marking bytes literals with a ``b`` prefix |
| 348 | and then designating textual data with a ``u`` prefix or using the |
| 349 | ``unicode_literals`` future statement. |
| 350 | |
R David Murray | 790e005 | 2012-04-23 14:44:00 -0400 | [diff] [blame] | 351 | If your project supports versions of Python predating 2.6, then you should use |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 352 | the six_ project and its ``b()`` function to denote bytes literals. For text |
| 353 | literals you can either use six's ``u()`` function or use a ``u`` prefix. |
| 354 | |
| 355 | |
| 356 | Decide what APIs Will Accept |
| 357 | **************************** |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 358 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 359 | In Python 2 it was very easy to accidentally create an API that accepted both |
| 360 | bytes and textual data. But in Python 3, thanks to the more strict handling of |
| 361 | disparate types, this loose usage of bytes and text together tends to fail. |
| 362 | |
| 363 | Take the dict ``{b'a': 'bytes', u'a': 'text'}`` in Python 2.6. It creates the |
| 364 | dict ``{u'a': 'text'}`` since ``b'a' == u'a'``. But in Python 3 the equivalent |
| 365 | dict creates ``{b'a': 'bytes', 'a': 'text'}``, i.e., no lost data. Similar |
| 366 | issues can crop up when transitioning Python 2 code to Python 3. |
| 367 | |
| 368 | This means you need to choose what an API is going to accept and create and |
| 369 | consistently stick to that API in both Python 2 and 3. |
| 370 | |
| 371 | |
Brett Cannon | ce71ab2 | 2011-02-05 22:05:05 +0000 | [diff] [blame] | 372 | Bytes / Unicode Comparison |
Antoine Pitrou | 8d8f7c5 | 2011-02-05 11:40:05 +0000 | [diff] [blame] | 373 | ************************** |
| 374 | |
| 375 | In Python 3, mixing bytes and unicode is forbidden in most situations; it |
| 376 | will raise a :class:`TypeError` where Python 2 would have attempted an implicit |
| 377 | coercion between types. However, there is one case where it doesn't and |
| 378 | it can be very misleading:: |
| 379 | |
| 380 | >>> b"" == "" |
| 381 | False |
| 382 | |
Brett Cannon | a2f1544 | 2011-02-09 22:55:13 +0000 | [diff] [blame] | 383 | This is because an equality comparison is required by the language to always |
Antoine Pitrou | 8d8f7c5 | 2011-02-05 11:40:05 +0000 | [diff] [blame] | 384 | succeed (and return ``False`` for incompatible types). However, this also |
| 385 | means that code incorrectly ported to Python 3 can display buggy behaviour |
| 386 | if such comparisons are silently executed. To detect such situations, |
| 387 | Python 3 has a ``-b`` flag that will display a warning:: |
| 388 | |
| 389 | $ python3 -b |
| 390 | >>> b"" == "" |
| 391 | __main__:1: BytesWarning: Comparison between bytes and string |
| 392 | False |
| 393 | |
| 394 | To turn the warning into an exception, use the ``-bb`` flag instead:: |
| 395 | |
| 396 | $ python3 -bb |
| 397 | >>> b"" == "" |
| 398 | Traceback (most recent call last): |
| 399 | File "<stdin>", line 1, in <module> |
| 400 | BytesWarning: Comparison between bytes and string |
| 401 | |
| 402 | |
Antoine Pitrou | bd866e9 | 2011-02-05 12:13:38 +0000 | [diff] [blame] | 403 | Indexing bytes objects |
| 404 | '''''''''''''''''''''' |
| 405 | |
| 406 | Another potentially surprising change is the indexing behaviour of bytes |
| 407 | objects in Python 3:: |
| 408 | |
| 409 | >>> b"xyz"[0] |
| 410 | 120 |
| 411 | |
| 412 | Indeed, Python 3 bytes objects (as well as :class:`bytearray` objects) |
| 413 | are sequences of integers. But code converted from Python 2 will often |
| 414 | assume that indexing a bytestring produces another bytestring, not an |
| 415 | integer. To reconcile both behaviours, use slicing:: |
| 416 | |
| 417 | >>> b"xyz"[0:1] |
| 418 | b'x' |
| 419 | >>> n = 1 |
| 420 | >>> b"xyz"[n:n+1] |
| 421 | b'y' |
| 422 | |
| 423 | The only remaining gotcha is that an out-of-bounds slice returns an empty |
| 424 | bytes object instead of raising ``IndexError``: |
| 425 | |
| 426 | >>> b"xyz"[3] |
| 427 | Traceback (most recent call last): |
| 428 | File "<stdin>", line 1, in <module> |
| 429 | IndexError: index out of range |
| 430 | >>> b"xyz"[3:4] |
| 431 | b'' |
| 432 | |
| 433 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 434 | ``__str__()``/``__unicode__()`` |
| 435 | ''''''''''''''''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 436 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 437 | In Python 2, objects can specify both a string and unicode representation of |
| 438 | themselves. In Python 3, though, there is only a string representation. This |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 439 | becomes an issue as people can inadvertently do things in their ``__str__()`` |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 440 | methods which have unpredictable results (e.g., infinite recursion if you |
| 441 | happen to use the ``unicode(self).encode('utf8')`` idiom as the body of your |
| 442 | ``__str__()`` method). |
| 443 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 444 | You can use a mixin class to work around this. This allows you to only define a |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 445 | ``__unicode__()`` method for your class and let the mixin derive |
| 446 | ``__str__()`` for you (code from |
| 447 | http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/):: |
| 448 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 449 | import sys |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 450 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 451 | class UnicodeMixin(object): |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 452 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 453 | """Mixin class to handle defining the proper __str__/__unicode__ |
| 454 | methods in Python 2 or 3.""" |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 455 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 456 | if sys.version_info[0] >= 3: # Python 3 |
| 457 | def __str__(self): |
| 458 | return self.__unicode__() |
| 459 | else: # Python 2 |
| 460 | def __str__(self): |
| 461 | return self.__unicode__().encode('utf8') |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 462 | |
| 463 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 464 | class Spam(UnicodeMixin): |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 465 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 466 | def __unicode__(self): |
| 467 | return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 468 | |
| 469 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 470 | Don't Index on Exceptions |
| 471 | ''''''''''''''''''''''''' |
Antoine Pitrou | 5c28cfdc | 2011-02-05 11:53:39 +0000 | [diff] [blame] | 472 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 473 | In Python 2, the following worked:: |
| 474 | |
Brett Cannon | 4b0c24a | 2011-02-03 22:14:58 +0000 | [diff] [blame] | 475 | >>> exc = Exception(1, 2, 3) |
| 476 | >>> exc.args[1] |
| 477 | 2 |
| 478 | >>> exc[1] # Python 2 only! |
| 479 | 2 |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 480 | |
Eli Bendersky | 7ac3419 | 2011-02-07 04:44:19 +0000 | [diff] [blame] | 481 | But in Python 3, indexing directly on an exception is an error. You need to |
| 482 | make sure to only index on the :attr:`BaseException.args` attribute which is a |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 483 | sequence containing all arguments passed to the :meth:`__init__` method. |
| 484 | |
Eli Bendersky | 7ac3419 | 2011-02-07 04:44:19 +0000 | [diff] [blame] | 485 | Even better is to use the documented attributes the exception provides. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 486 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 487 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 488 | Don't use ``__getslice__`` & Friends |
| 489 | '''''''''''''''''''''''''''''''''''' |
Antoine Pitrou | 5c28cfdc | 2011-02-05 11:53:39 +0000 | [diff] [blame] | 490 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 491 | Been deprecated for a while, but Python 3 finally drops support for |
| 492 | ``__getslice__()``, etc. Move completely over to :meth:`__getitem__` and |
| 493 | friends. |
| 494 | |
| 495 | |
Brett Cannon | 45aa7cc | 2011-02-05 22:16:40 +0000 | [diff] [blame] | 496 | Updating doctests |
| 497 | ''''''''''''''''' |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 498 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 499 | Don't forget to make them Python 2/3 compatible as well. If you wrote a |
| 500 | monolithic set of doctests (e.g., a single docstring containing all of your |
| 501 | doctests), you should at least consider breaking the doctests up into smaller |
| 502 | pieces to make it more manageable to fix. Otherwise it might very well be worth |
| 503 | your time and effort to port your tests to :mod:`unittest`. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 504 | |
| 505 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 506 | Update ``map`` for imbalanced input sequences |
| 507 | ''''''''''''''''''''''''''''''''''''''''''''' |
Jason R. Coombs | a90e364 | 2011-12-03 08:24:21 -0500 | [diff] [blame] | 508 | |
Brett Cannon | f0996a9 | 2014-01-17 11:45:01 -0500 | [diff] [blame] | 509 | With Python 2, when ``map`` was given more than one input sequence it would pad |
| 510 | the shorter sequences with `None` values, returning a sequence as long as the |
| 511 | longest input sequence. |
Jason R. Coombs | a90e364 | 2011-12-03 08:24:21 -0500 | [diff] [blame] | 512 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 513 | With Python 3, if the input sequences to ``map`` are of unequal length, ``map`` |
Jason R. Coombs | a90e364 | 2011-12-03 08:24:21 -0500 | [diff] [blame] | 514 | will stop at the termination of the shortest of the sequences. For full |
Brett Cannon | f0996a9 | 2014-01-17 11:45:01 -0500 | [diff] [blame] | 515 | compatibility with ``map`` from Python 2.x, wrap the sequence arguments in |
Jason R. Coombs | a90e364 | 2011-12-03 08:24:21 -0500 | [diff] [blame] | 516 | :func:`itertools.zip_longest`, e.g. ``map(func, *sequences)`` becomes |
| 517 | ``list(map(func, itertools.zip_longest(*sequences)))``. |
| 518 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 519 | Eliminate ``-3`` Warnings |
| 520 | ------------------------- |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 521 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 522 | When you run your application's test suite, run it using the ``-3`` flag passed |
| 523 | to Python. This will cause various warnings to be raised during execution about |
Brett Cannon | f0996a9 | 2014-01-17 11:45:01 -0500 | [diff] [blame] | 524 | things that are semantic changes between Python 2 and 3. Try to eliminate those |
| 525 | warnings to make your code even more portable to Python 3. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 526 | |
| 527 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 528 | Alternative Approaches |
| 529 | ====================== |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 530 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 531 | While supporting Python 2 & 3 simultaneously is typically the preferred choice |
| 532 | by people so that they can continue to improve code and have it work for the |
| 533 | most number of users, your life may be easier if you only have to support one |
| 534 | major version of Python going forward. |
| 535 | |
| 536 | Supporting Only Python 3 Going Forward From Python 2 Code |
| 537 | --------------------------------------------------------- |
| 538 | |
| 539 | If you have Python 2 code but going forward only want to improve it as Python 3 |
| 540 | code, then you can use 2to3_ to translate your Python 2 code to Python 3 code. |
| 541 | This is only recommended, though, if your current version of your project is |
| 542 | going into maintenance mode and you want all new features to be exclusive to |
| 543 | Python 3. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 544 | |
| 545 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 546 | Backporting Python 3 code to Python 2 |
| 547 | ------------------------------------- |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 548 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 549 | If you have Python 3 code and have little interest in supporting Python 2 you |
| 550 | can use 3to2_ to translate from Python 3 code to Python 2 code. This is only |
| 551 | recommended if you don't plan to heavily support Python 2 users. |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 552 | |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 553 | |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 554 | Other Resources |
| 555 | =============== |
Georg Brandl | 2cb2fa9 | 2011-02-07 15:30:45 +0000 | [diff] [blame] | 556 | |
Brett Cannon | 6277fa4 | 2011-02-18 01:34:28 +0000 | [diff] [blame] | 557 | The authors of the following blog posts, wiki pages, and books deserve special |
| 558 | thanks for making public their tips for porting Python 2 code to Python 3 (and |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 559 | thus helping provide information for this document and its various revisions |
| 560 | over the years): |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 561 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 562 | * http://wiki.python.org/moin/PortingPythonToPy3k |
Brett Cannon | 6277fa4 | 2011-02-18 01:34:28 +0000 | [diff] [blame] | 563 | * http://python3porting.com/ |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 564 | * http://docs.pythonsprints.com/python3_porting/py-porting.html |
| 565 | * http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/ |
| 566 | * http://dabeaz.blogspot.com/2011/01/porting-py65-and-my-superboard-to.html |
| 567 | * http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/ |
| 568 | * http://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide/ |
Brett Cannon | b7e6b89 | 2013-03-09 14:22:35 -0500 | [diff] [blame] | 569 | * https://wiki.ubuntu.com/Python/3 |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 570 | |
| 571 | If you feel there is something missing from this document that should be added, |
| 572 | please email the python-porting_ mailing list. |
| 573 | |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 574 | |
| 575 | |
| 576 | .. _2to3: http://docs.python.org/2/library/2to3.html |
| 577 | .. _3to2: https://pypi.python.org/pypi/3to2 |
| 578 | .. _Cheeseshop: PyPI_ |
| 579 | .. _coverage: https://pypi.python.org/pypi/coverage |
| 580 | .. _future: http://python-future.org/ |
| 581 | .. _modernize: https://github.com/mitsuhiko/python-modernize |
| 582 | .. _Porting to Python 3: http://python3porting.com/ |
| 583 | .. _PyPI: http://pypi.python.org/ |
| 584 | .. _Python 2.2: http://www.python.org/2.2.x |
| 585 | .. _Python 2.5: http://www.python.org/2.5.x |
| 586 | .. _Python 2.6: http://www.python.org/2.6.x |
| 587 | .. _Python 2.7: http://www.python.org/2.7.x |
| 588 | .. _Python 2.5: http://www.python.org/2.5.x |
| 589 | .. _Python 3.3: http://www.python.org/3.3.x |
| 590 | .. _Python 3 Packages: https://pypi.python.org/pypi?:action=browse&c=533&show=all |
| 591 | .. _Python 3 Q & A: http://ncoghlan-devs-python-notes.readthedocs.org/en/latest/python3/questions_and_answers.html |
Brett Cannon | 8045d97 | 2011-02-03 22:01:54 +0000 | [diff] [blame] | 592 | .. _python-porting: http://mail.python.org/mailman/listinfo/python-porting |
Brett Cannon | 9ca21b3 | 2014-01-07 11:52:04 -0500 | [diff] [blame] | 593 | .. _six: https://pypi.python.org/pypi/six |
| 594 | .. _tox: https://pypi.python.org/pypi/tox |
| 595 | .. _trove classifiers: https://pypi.python.org/pypi?%3Aaction=list_classifiers |
| 596 | |