Lines Matching refs:Python
4 Porting Python 2 Code to Python 3
11 With Python 3 being the future of Python while Python 2 is still in active
13 Python. This guide is meant to help you figure out how best to support both
14 Python 2 & 3 simultaneously.
16 If you are looking to port an extension module instead of pure Python code,
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`_ or
21 Brett Cannon's `Why Python 3 exists`_.
29 To make your project be single-source Python 2/3 compatible, the basic steps
32 #. Only worry about supporting Python 2.7
35 #. Learn the differences between Python 2 & 3
37 #. Use Pylint_ to help make sure you don't regress on your Python 3 support
40 use of Python 3 (``pip install caniusepython3``)
42 to make sure you stay compatible with Python 2 & 3 (tox_ can help test
43 against multiple versions of Python; ``pip install tox``)
45 works in both Python 2 & 3 (e.g. use mypy_ to check your typing under both
46 Python 2 & Python 3).
52 A key point about supporting Python 2 & 3 simultaneously is that you can start
53 **today**! Even if your dependencies are not supporting Python 3 yet that does
54 not mean you can't modernize your code **now** to support Python 3. Most changes
55 required to support Python 3 lead to cleaner code using newer practices even in
56 Python 2 code.
58 Another key point is that modernizing your Python 2 code to also support
59 Python 3 is largely automated for you. While you might have to make some API
60 decisions thanks to Python 3 clarifying text data versus binary data, the
65 your code to support Python 2 & 3 simultaneously.
68 Drop support for Python 2.6 and older
71 While you can make Python 2.5 work with Python 3, it is **much** easier if you
72 only have to work with Python 2.7. If dropping Python 2.5 is not an
73 option then the six_ project can help you support Python 2.5 & 3 simultaneously
77 If you are able to skip Python 2.5 and older, then the required changes
78 to your code should continue to look and feel like idiomatic Python code. At
83 But you should aim for only supporting Python 2.7. Python 2.6 is no longer
85 to work around any issues you come across with Python 2.6. There are also some
86 tools mentioned in this HOWTO which do not support Python 2.6 (e.g., Pylint_),
88 for you if you only support the versions of Python that you have to support.
95 specifying what versions of Python you support. As your project does not support
96 Python 3 yet you should at least have
97 ``Programming Language :: Python :: 2 :: Only`` specified. Ideally you should
98 also specify each major/minor version of Python that you do support, e.g.
99 ``Programming Language :: Python :: 2.7``.
105 Once you have your code supporting the oldest version of Python 2 you want it
115 Learn the differences between Python 2 & 3
119 Python 3! But to fully understand how your code is going to change and what
121 Python 3 makes in terms of Python 2. Typically the two best ways of doing that
122 is reading the `"What's New"`_ doc for each release of Python 3 and the
123 `Porting to Python 3`_ book (which is free online). There is also a handy
124 `cheat sheet`_ from the Python-Future project.
130 Once you feel like you know what is different in Python 3 compared to Python 2,
133 depend on how much like Python 3 you want your code to be. Futurize_ does its
134 best to make Python 3 idioms and practices exist in Python 2, e.g. backporting
135 the ``bytes`` type from Python 3 so that you have semantic parity between the
136 major versions of Python. Modernize_,
137 on the other hand, is more conservative and targets a Python 2/3 subset of
138 Python, directly relying on six_ to help provide compatibility. As Python 3 is
140 practices that Python 3 introduces which you are not accustomed to yet.
143 Python 3 while staying compatible with the version of Python 2 you started with.
151 Python 3 and so there are a handful of things you will need to update manually
152 to get full Python 3 support (which of these steps are necessary vary between
164 In Python 3, ``5 / 2 == 2.5`` and not ``2``; all division between ``int`` values
165 result in a ``float``. This change has actually been planned since Python 2.2
185 In Python 2 you could use the ``str`` type for both text and binary data.
195 pronounced, Python 3 did what most languages created in the age of the internet
197 mixed together (Python predates widespread access to the internet). For any code
206 do well). In Python 2 this means making sure the APIs that take text can work
208 ``bytes`` type from Python 3 (which is a subset of ``str`` in Python 2 and acts
209 as an alias for ``bytes`` type in Python 2). Usually the biggest issue is
210 realizing which methods exist on which types in Python 2 & 3 simultaneously
211 (for text that's ``unicode`` in Python 2 and ``str`` in Python 3, for binary
212 that's ``str``/``bytes`` in Python 2 and ``bytes`` in Python 3). The following
213 table lists the **unique** methods of each data type across Python 2 & 3
215 either Python 2 or 3, but it can't be used by the textual data type consistently
216 between Python 2 and 3 because ``str`` in Python 3 doesn't have the method). Do
217 note that as of Python 3.5 the ``__mod__`` method was added to the bytes type.
250 binary reading). Under Python 3, binary files and text files are clearly
256 module is consistent from Python 2 to 3 while the built-in :func:`open` function
257 is not (in Python 3 it's actually :func:`io.open`). Do not bother with the
259 keeping compatibility with Python 2.5.
262 same arguments between Python 2 & 3. Passing an integer to ``bytes`` in Python 2
264 But in Python 3, an integer argument to ``bytes`` will give you a bytes object
267 bytes object to ``str``. In Python 2 you just get the bytes object back:
268 ``str(b'3') == b'3'``. But in Python 3 you get the string representation of the
272 **not** require any special handling). In Python 2,
273 ``b'123'[1] == b'2'`` while in Python 3 ``b'123'[1] == 50``. Because binary data
274 is simply a collection of binary numbers, Python 3 returns the integer value for
275 the byte you index on. But in Python 2 because ``bytes == str``, indexing
277 named ``six.indexbytes()`` which will return an integer like in Python 3:
284 code for binary data works with ``bytes`` in Python 2 (see the table above
299 version of Python is running. The best way to do this is with feature detection
300 of whether the version of Python you're running under supports what you need.
302 against Python 2 and not Python 3. To help explain this, let's look at an
306 is available in Python's standard library since Python 3.3 and available for
307 Python 2 through importlib2_ on PyPI. You might be tempted to write code to
317 The problem with this code is what happens when Python 4 comes out? It would
318 be better to treat Python 2 as the exceptional case instead of Python 3 and
319 assume that future Python versions will be more compatible with Python 3 than
320 Python 2::
342 Once you have fully translated your code to be compatible with Python 3, you
344 Python 3. This is especially true if you have a dependency which is blocking you
345 from actually running under Python 3 at the moment.
354 You can also run Python 2 with the ``-3`` flag to be warned about various
360 to receive warnings when your code begins to deviate from Python 3
363 you only support Python 2.7 and Python 3.4 or newer as that is Pylint's
364 minimum Python version support.
370 **After** you have made your code compatible with Python 3 you should begin to
373 -- directly or indirectly -- are blocking you from supporting Python 3. There
379 you from using Python 3. This allows you to avoid having to manually check your
380 dependencies and to be notified quickly when you can start running on Python 3.
383 Update your ``setup.py`` file to denote Python 3 compatibility
386 Once your code works under Python 3, you should update the classifiers in
387 your ``setup.py`` to contain ``Programming Language :: Python :: 3`` and to not
388 specify sole Python 2 support. This will tell anyone using your code that you
389 support Python 2 **and** 3. Ideally you will also want to add classifiers for
390 each major/minor version of Python you now support.
396 Once you are able to fully run under Python 3 you will want to make sure your
397 code always works under both Python 2 & 3. Probably the best tool for running
398 your tests under multiple Python interpreters is tox_. You can then integrate
400 Python 2 or 3 support.
402 You may also want to use the ``-bb`` flag with the Python 3 interpreter to
404 (the latter is available starting in Python 3.5). By default type-differing
411 Python 2 and 3 simultaneously. Your testing will also be set up so that you
412 don't accidentally break Python 2 or 3 compatibility regardless of which version
421 if it's being run under Python 2, then you can run the tool a second time as if
422 your code is running under Python 3. By running a static type checker twice like
424 of Python compared to another. If you add optional type hints to your code you
426 to make sure everything functions as expected in both versions of Python.
438 .. _Porting to Python 3: http://python3porting.com/
452 .. _Why Python 3 exists: https://snarky.ca/why-python-3-exists