Python 2 is End of Life – time to update your app!

2

Casey

March 5, 2020
Web Dev

Python 2 has been marked as EOL as of January 1, 2020, largely due to it’s less than ideal string handling methods. If you haven’t migrated your code from Python 2 to Python 3 yet, be aware that you’re susceptible to bugs and security issues which may arise while community and volunteer support shifts exclusively to Python 3.

Besides avoiding potential risk by upgrading, there are many things you can look forward to in Python 3, such as wider iterator adoption for common APIs, format strings, the ‘nonlocal’ keyword, etc. When you’re ready to migrate your apps to Python 3 it’s important to learn the major differences between Python 2 and 3, ensure strong test coverage, and consider implementing continuous integration if you’ve not already done so.

Background

In 2008 the Python Software Foundation first announced that Python 2 would be marked EOL in 2015. Some effort was taken to get users to upgrade prior to 2015, but this was considered largely unsuccessful. When 2014 came along the Python Software Foundation extended the deadline to 2020, and as of January 1, 2020, Python 2 is officially EOL.

In Python 2, a string of the form ‘abcd’ could be interpreted as a unicode or byte string depending on the context and intention (or lack thereof) of the developer. Multiple interpretations for a single object does not adhere to the Zen of Python‘s guidance which states: “there should be one — and preferably only one — obvious way to do it”. This was the driving force behind the backwards incompatible changes introduced in the development of Python 3. Along with other enhancements to the language, Python 3 treats all string objects as unicode by default.

Risks

Neglecting to upgrade your Python 2 applications to Python 3 may seem like the path of least resistance, but be aware that Python 2 being EOL means that any major bugs or security issues found in Python 2 itself or Python 2 software are not guaranteed to be fixed by volunteers in the community. Some may still assist for a time, but the number of those willing to help is expected to quickly dwindle.

Rewards

Aside from unicode strings by default, once you upgrade your app to Python 3 you can look forward to:

  • Print being a function with keyword arguments
  • APIs returning views and iterators instead of lists
  • Simplified ordering comparisons (i.e. <, <=, >=, >)
  • Integer operations such as “1/2” returning floating point numbers
  • ‘nonlocal’ statement introduced for mutating variable defined outside local scope
  • Format strings
  • And much more!

Migration Tips & Tricks

Migrating from Python 2 to 3 can be easier than it sounds, but does require due diligence. Some general guidance for making the transition is:

  • Make sure you have good test coverage (coverage.py can help)
  • Learn the differences between Python 2 & 3
  • Use Futurize (or Modernize) to update your code
  • Use Pylint to help make sure you don’t regress on your Python 3 support
  • Use caniusepython3 to determine dependencies blocking use of Python 3
  • Use continuous integration to make sure you stay compatible with Python 2 & 3 (tox can help test against multiple versions of Python)
  • Consider using optional static type checking to make sure your type usage works in both Python 2 & 3 (e.g. use mypy to check your typing under both Python 2 & Python 3)

tl;dr

Now that January 1, 2020 has passed, Python 2 is officially without support and Python 3 is the default. Continued maintenance of Python 2 code comes with significant risks, mainly from the lack of support in the face of bugs and/or security issues, so proceed with caution. Risk avoidance aside, Python 3 features like wider iterator adoption for common APIs, format strings, the ‘nonlocal’ keyword, and many other enhancements are reason enough to make the switch. Of course, when you’re ready to migrate your apps to Python 3, some key tips to try to remember are: learn the major differences between Python 2 and 3, ensure strong test coverage, and implement continuous integration if you haven’t already!

Casey