How to upgrade a Python 2 codebase to Python 3
Let’s stop using Python 2 from now on
Python 2.7 is not supported after Jan. 1st, 2020. Since its first release on July 3, 2010, Python 2.7 has been active for 10 years and a large number of projects are written in this version. Most Python 2 projects should still be able to work properly as Python 2.7 is a very stable version and most bugs have been fixed in the past 10 years. However, with Python 2.7, you are not able to use many cool features introduced in Python 3, such as f-string, typing, and assignment expression. Particularly, typing has become a must-to-have for new Python projects because it can make the code more readable. Besides, more importantly, many packages do not support Python 2.7 anymore, which will be annoying when you keep maintaining your codebase in Python 2.7. Therefore, as a developer, you may have the task to upgrade your Python 2 codebase to Python 3.
The future
module can be used to make Python 2 code compatible with both the Python 2 and Python 3 platforms (Py2/3). The future
module is based on modules lib2to3
, six
and python-modernize
, which provides basic functionality to make Python 2 code work in Python 3. The future
module acts as a wrapper for the fixers of these modules and is thus easier to use. The futurize
script provided by the future
module passes Python 2 code through…