Porting Python 2 Code to Python 3 Release 3.9.5 Guido van Rossum and the Python development team June 01, 2021 Python Software Foundation Email: docs@python.org Contents Abstract With Python 3 being the future of Python while Python 2 is still in active use, it is good to have your project available for both major releases of Python. This guide is meant to help you figure out how best to support both Python 2 & 3 simultaneously. If you are looking to port an extension module instead of pure Python code, please see cporting-howto. If you would like to read one core Python developer’s take on why Python 3 came into existence, you can read Nick Coghlan’s Python 3 Q & A or Brett Cannon’s Why Python 3 exists. For help with porting, you can email the python-porting mailing list with questions. 1 author Brett Cannon 1 The Short Explanation 2 2 Details 2 2.1 Drop support for Python 2.6 and older . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.2 Make sure you specify the proper version support in your$_{setup.py}$ file . . . . . . . . . . . . . . 3 2.3 Have good test coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.4 Learn the differences between Python 2 & 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.5 Update your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.6 Prevent compatibility regressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.7 Check which dependencies block your transition . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.8 Update your$_{setup.py}$ file to denote Python 3 compatibility . . . . . . . . . . . . . . . . . . . . 6 2.9 Use continuous integration to stay compatible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.10 Consider using optional static type checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7