README.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. .. _overview:
  2. Overview: Easy, clean, reliable Python 2/3 compatibility
  3. ========================================================
  4. .. image:: https://github.com/PythonCharmers/python-future/actions/workflows/ci.yml/badge.svg?branch=master
  5. :target: https://github.com/PythonCharmers/python-future/actions/workflows/ci.yml?query=branch%3Amaster
  6. ``python-future`` is the missing compatibility layer between Python 2 and
  7. Python 3. It allows you to use a single, clean Python 3.x-compatible
  8. codebase to support both Python 2 and Python 3 with minimal overhead.
  9. It provides ``future`` and ``past`` packages with backports and forward
  10. ports of features from Python 3 and 2. It also comes with ``futurize`` and
  11. ``pasteurize``, customized 2to3-based scripts that helps you to convert
  12. either Py2 or Py3 code easily to support both Python 2 and 3 in a single
  13. clean Py3-style codebase, module by module.
  14. The ``python-future`` project has been downloaded over 1.7 billion times.
  15. .. _status
  16. Status
  17. ------
  18. The ``python-future`` project was created in 2013 to attempt to save Python from
  19. the schism of version incompatibility that was threatening to tear apart the
  20. language (as Perl 6 contributed to the death of Perl).
  21. That time is now past. Thanks to a huge porting effort across the Python
  22. community, Python 3 eventually thrived. Python 2 reached its end of life in
  23. 2020 and the ``python-future`` package should no longer be necessary. Use it to
  24. help with porting legacy code to Python 3 but don't depend on it for new code.
  25. .. _features:
  26. Features
  27. --------
  28. - ``future.builtins`` package (also available as ``builtins`` on Py2) provides
  29. backports and remappings for 20 builtins with different semantics on Py3
  30. versus Py2
  31. - support for directly importing 30 standard library modules under
  32. their Python 3 names on Py2
  33. - support for importing the other 14 refactored standard library modules
  34. under their Py3 names relatively cleanly via
  35. ``future.standard_library`` and ``future.moves``
  36. - ``past.builtins`` package provides forward-ports of 19 Python 2 types and
  37. builtin functions. These can aid with per-module code migrations.
  38. - ``past.translation`` package supports transparent translation of Python 2
  39. modules to Python 3 upon import. [This feature is currently in alpha.]
  40. - 1000+ unit tests, including many from the Py3.3 source tree.
  41. - ``futurize`` and ``pasteurize`` scripts based on ``2to3`` and parts of
  42. ``3to2`` and ``python-modernize``, for automatic conversion from either Py2
  43. or Py3 to a clean single-source codebase compatible with Python 2.6+ and
  44. Python 3.3+.
  45. - a curated set of utility functions and decorators in ``future.utils`` and
  46. ``past.utils`` selected from Py2/3 compatibility interfaces from projects
  47. like ``six``, ``IPython``, ``Jinja2``, ``Django``, and ``Pandas``.
  48. - support for the ``surrogateescape`` error handler when encoding and
  49. decoding the backported ``str`` and ``bytes`` objects. [This feature is
  50. currently in alpha.]
  51. - support for pre-commit hooks
  52. .. _code-examples:
  53. Code examples
  54. -------------
  55. Replacements for Py2's built-in functions and types are designed to be imported
  56. at the top of each Python module together with Python's built-in ``__future__``
  57. statements. For example, this code behaves identically on Python 2.6/2.7 after
  58. these imports as it does on Python 3.3+:
  59. .. code-block:: python
  60. from __future__ import absolute_import, division, print_function
  61. from builtins import (bytes, str, open, super, range,
  62. zip, round, input, int, pow, object)
  63. # Backported Py3 bytes object
  64. b = bytes(b'ABCD')
  65. assert list(b) == [65, 66, 67, 68]
  66. assert repr(b) == "b'ABCD'"
  67. # These raise TypeErrors:
  68. # b + u'EFGH'
  69. # bytes(b',').join([u'Fred', u'Bill'])
  70. # Backported Py3 str object
  71. s = str(u'ABCD')
  72. assert s != bytes(b'ABCD')
  73. assert isinstance(s.encode('utf-8'), bytes)
  74. assert isinstance(b.decode('utf-8'), str)
  75. assert repr(s) == "'ABCD'" # consistent repr with Py3 (no u prefix)
  76. # These raise TypeErrors:
  77. # bytes(b'B') in s
  78. # s.find(bytes(b'A'))
  79. # Extra arguments for the open() function
  80. f = open('japanese.txt', encoding='utf-8', errors='replace')
  81. # New zero-argument super() function:
  82. class VerboseList(list):
  83. def append(self, item):
  84. print('Adding an item')
  85. super().append(item)
  86. # New iterable range object with slicing support
  87. for i in range(10**15)[:10]:
  88. pass
  89. # Other iterators: map, zip, filter
  90. my_iter = zip(range(3), ['a', 'b', 'c'])
  91. assert my_iter != list(my_iter)
  92. # The round() function behaves as it does in Python 3, using
  93. # "Banker's Rounding" to the nearest even last digit:
  94. assert round(0.1250, 2) == 0.12
  95. # input() replaces Py2's raw_input() (with no eval()):
  96. name = input('What is your name? ')
  97. print('Hello ' + name)
  98. # pow() supports fractional exponents of negative numbers like in Py3:
  99. z = pow(-1, 0.5)
  100. # Compatible output from isinstance() across Py2/3:
  101. assert isinstance(2**64, int) # long integers
  102. assert isinstance(u'blah', str)
  103. assert isinstance('blah', str) # only if unicode_literals is in effect
  104. # Py3-style iterators written as new-style classes (subclasses of
  105. # future.types.newobject) are automatically backward compatible with Py2:
  106. class Upper(object):
  107. def __init__(self, iterable):
  108. self._iter = iter(iterable)
  109. def __next__(self): # note the Py3 interface
  110. return next(self._iter).upper()
  111. def __iter__(self):
  112. return self
  113. assert list(Upper('hello')) == list('HELLO')
  114. There is also support for renamed standard library modules. The recommended
  115. interface works like this:
  116. .. code-block:: python
  117. # Many Py3 module names are supported directly on both Py2.x and 3.x:
  118. from http.client import HttpConnection
  119. import html.parser
  120. import queue
  121. import xmlrpc.client
  122. # Refactored modules with clashing names on Py2 and Py3 are supported
  123. # as follows:
  124. from future import standard_library
  125. standard_library.install_aliases()
  126. # Then, for example:
  127. from itertools import filterfalse, zip_longest
  128. from urllib.request import urlopen
  129. from collections import ChainMap
  130. from collections import UserDict, UserList, UserString
  131. from subprocess import getoutput, getstatusoutput
  132. from collections import Counter, OrderedDict # backported to Py2.6
  133. Automatic conversion to Py2/3-compatible code
  134. ---------------------------------------------
  135. ``python-future`` comes with two scripts called ``futurize`` and
  136. ``pasteurize`` to aid in making Python 2 code or Python 3 code compatible with
  137. both platforms (Py2/3). It is based on 2to3 and uses fixers from ``lib2to3``,
  138. ``lib3to2``, and ``python-modernize``, as well as custom fixers.
  139. ``futurize`` passes Python 2 code through all the appropriate fixers to turn it
  140. into valid Python 3 code, and then adds ``__future__`` and ``future`` package
  141. imports so that it also runs under Python 2.
  142. For conversions from Python 3 code to Py2/3, use the ``pasteurize`` script
  143. instead. This converts Py3-only constructs (e.g. new metaclass syntax) to
  144. Py2/3 compatible constructs and adds ``__future__`` and ``future`` imports to
  145. the top of each module.
  146. In both cases, the result should be relatively clean Py3-style code that runs
  147. mostly unchanged on both Python 2 and Python 3.
  148. Futurize: 2 to both
  149. ~~~~~~~~~~~~~~~~~~~
  150. For example, running ``futurize -w mymodule.py`` turns this Python 2 code:
  151. .. code-block:: python
  152. import Queue
  153. from urllib2 import urlopen
  154. def greet(name):
  155. print 'Hello',
  156. print name
  157. print "What's your name?",
  158. name = raw_input()
  159. greet(name)
  160. into this code which runs on both Py2 and Py3:
  161. .. code-block:: python
  162. from __future__ import print_function
  163. from future import standard_library
  164. standard_library.install_aliases()
  165. from builtins import input
  166. import queue
  167. from urllib.request import urlopen
  168. def greet(name):
  169. print('Hello', end=' ')
  170. print(name)
  171. print("What's your name?", end=' ')
  172. name = input()
  173. greet(name)
  174. The first four lines have no effect under Python 3 and can be removed from
  175. the codebase when Python 2 compatibility is no longer required.
  176. See :ref:`forwards-conversion` and :ref:`backwards-conversion` for more details.
  177. Automatic translation
  178. ~~~~~~~~~~~~~~~~~~~~~
  179. The ``past`` package can automatically translate some simple Python 2
  180. modules to Python 3 upon import. The goal is to support the "long tail" of
  181. real-world Python 2 modules (e.g. on PyPI) that have not been ported yet. For
  182. example, here is how to use a Python 2-only package called ``plotrique`` on
  183. Python 3. First install it:
  184. .. code-block:: bash
  185. $ pip3 install plotrique==0.2.5-7 --no-compile # to ignore SyntaxErrors
  186. (or use ``pip`` if this points to your Py3 environment.)
  187. Then pass a whitelist of module name prefixes to the ``autotranslate()`` function.
  188. Example:
  189. .. code-block:: bash
  190. $ python3
  191. >>> from past.translation import autotranslate
  192. >>> autotranslate(['plotrique'])
  193. >>> import plotrique
  194. This transparently translates and runs the ``plotrique`` module and any
  195. submodules in the ``plotrique`` package that ``plotrique`` imports.
  196. This is intended to help you migrate to Python 3 without the need for all
  197. your code's dependencies to support Python 3 yet. It should be used as a
  198. last resort; ideally Python 2-only dependencies should be ported
  199. properly to a Python 2/3 compatible codebase using a tool like
  200. ``futurize`` and the changes should be pushed to the upstream project.
  201. Note: the auto-translation feature is still in alpha; it needs more testing and
  202. development, and will likely never be perfect.
  203. Pre-commit hooks
  204. ~~~~~~~~~~~~~~~~
  205. `Pre-commit <https://pre-commit.com/>`_ is a framework for managing and maintaining
  206. multi-language pre-commit hooks.
  207. In case you need to port your project from Python 2 to Python 3, you might consider
  208. using such hook during the transition period.
  209. First:
  210. .. code-block:: bash
  211. $ pip install pre-commit
  212. and then in your project's directory:
  213. .. code-block:: bash
  214. $ pre-commit install
  215. Next, you need to add this entry to your ``.pre-commit-config.yaml``
  216. .. code-block:: yaml
  217. - repo: https://github.com/PythonCharmers/python-future
  218. rev: master
  219. hooks:
  220. - id: futurize
  221. args: [--both-stages]
  222. The ``args`` part is optional, by default only stage1 is applied.
  223. Licensing
  224. ---------
  225. :Author: Ed Schofield, Jordan M. Adler, et al
  226. :Copyright: 2013-2024 Python Charmers, Australia.
  227. :Sponsors: Python Charmers: https://pythoncharmers.com
  228. Pinterest https://opensource.pinterest.com
  229. :Licence: MIT. See ``LICENSE.txt`` or `here <https://python-future.org/credits.html>`_.
  230. :Other credits: See `here <https://python-future.org/credits.html>`_.
  231. Docs
  232. ----
  233. See the docs `here <https://python-future.org>`_.
  234. Next steps
  235. ----------
  236. If you are new to Python-Future, check out the `Quickstart Guide
  237. <https://python-future.org/quickstart.html>`_.
  238. For an update on changes in the latest version, see the `What's New
  239. <https://python-future.org/whatsnew.html>`_ page.