Simple Generators division PEP 238: Changing the Division Operator absolute_import PEP 328: Imports: Multi-Line and Absolute/Relative with_statement PEP 343: The “with” Statement print_function PEP 3105: Make print a function unicode_literals PEP 3112: Bytes literals in Python 3000 https://docs.python.org/2.7/library/__future__.html
futurize and pasteurize • Support for directly importing 30 standard library modules under their Python 3 names on Python 2 https://pypi.python.org/pypi/future
Python 2.7 • Using __future__ • Make sure you have good test coverage • Use pylint to help make sure you don’t regress on your Python 3 support • Use caniusepython3 to find out which of your dependencies are blocking Python 3 • tox can help test against multiple versions of Python • Use 2to3 to rewrite your code to run only under Python 3
or: from cStringIO import StringIO # Python 2 and 3: from io import BytesIO # for handling byte strings from io import StringIO # for handling unicode strings
# Idiomatic Python 3 for value in heights.values(): # extra memory overhead on Py2 ... # Python 2 and 3: option 2 from six import itervalues for key in itervalues(heights): ...
... # Python 2 and 3: option 1 for (key, value) in heights.items(): # inefficient on Py2 ... # Python 2 and 3: option 3 from six import iteritems for (key, value) in iteritems(heights): ...
# Python 3 only: class Form(BaseForm, metaclass=FormType): pass # Python 2 and 3: from six import with_metaclass class Form(with_metaclass(FormType, BaseForm)): pass