Everything you need to be prepared

The sunset of Python 2: What do IT leaders need to know?

Bart Copeland
© Shutterstock / GagoDesign  

Support for Python 2 will be cut off in 2020 and some may have already been feeling the pain! What are the next steps? How can you start preparing for migration to Python 3? In this article, Bart Copeland has some handy advice.

Python 2 will officially sunset in 2020, but users will likely start feeling some of the pain long before then.

Python 3 will become required for project updates long before we say our official adieu to Python 2 and support for projects will slowly start to decrease to just fixing bugs.

The upside is that almost all major open source Python packages now support both Python 3 and Python 2.7. But you’ll still be guaranteed friction.



Out with the old, in with the new

Python is one of the most popular programming languages in the world, owing largely to its ease of use and the number of tasks for which it’s ideally suited. Millions of developers around the world use Python’s flexibility to build software that ranges from powerful web applications to complex ML driven software.

Yet Python 2 falls short into two functional ways.

First off, Python 2 introduced two text types that were prone to misuse. Because Python 2’s string type does not clearly separate textual data from binary data, erroneous bugs were often introduced into the code. (It’s no surprise that a recent report ranked Python among the least secure programming languages.)

Secondly, Python 2 does not provide support for Unicode since it was created before the Unicode standard was finalized. As a result, it was left up to the developer to properly deal with different language encodings manually, which is prone to errors.

Python 3 resolves the two main problems with Python 2 by basing the default core string type on Unicode. This is a big gain for Python programmers because it eliminates the text/binary confusion and enables multilingual programming. The Python 3 series also gives programmers other wins, such as function annotations and range memory objects, which are significantly more memory efficient. But be aware that Python 3 also introduces a number of backward-incompatible changes, including:

  • Removal of classic Python 2 classes
  • Changing integer division to automatically generate a floating point result
  • Converting the print statement to a function

The last version of Python 2, Python 2.7, will cease to be actively maintained on Jan. 1, 2020. Enterprises with applications running under any version of Python 2 need to migrate – and the sooner the better.

SEE ALSO: Python crowned as most questioned language on StackOverflow

Advantages to migrating now

Such an important migration should not be pushed through at the last minute. Instead, migrating before the deadline will help organizations take the time needed to do it well, and account for any unforeseen glitches well in advance.

Here are the top six benefits to taking a more a proactive approach:

  • Improved compatibility: 94% of the top 360 most-downloaded packages already offer Python 3 support.
  • Ease of porting: Python 3.5 introduced changes that simplified code porting in September 2015.
  • Cloud support: The three largest public cloud providers fully support Python 3.
  • OS support: Many versions of Linux now install Python 3 by default.
  • Talent pool: Python 2 programmers can grow their skills to work on new corporate projects. This increases the in-house skill level for an organization while offering programmers the opportunity to continue learning, which encourages retention.  
  • Recruitment: Using Python 3 will help organizations be a more attractive workplace to other Python programmers.

To ensure a successful migration to the Python 3 series, here are three steps to take:

  1. Upgrade your existing code base to at least Python 2.6 and preferably Python 2.7.
    • You can also facilitate migration by running Python 2.7 in parallel with Python 3.
  2. Decide whether you need to continue supporting Python 2 or if you can just go straight to Python 3.
    • You’ll need to carefully evaluate the differences between the Python 2 series and the Python 3 series.
  3. Ensure you have a robust test suite in place to continually test for compatibility.

SEE ALSO: Dynaconf: Dynamic settings configuration for Python apps

Closing tips: Packages to help upgrade to Python 3

Before migrating, ensure you have a good understanding of Python 3 conventions. Then choose your tooling. There are currently three recommended packages: Six, 2T03 and Python-Future. Each has its pros and cons.

Six is best if you want to add Python 3 compatibility to your existing Python 2 code. Six provides a set of utilities that wrap over the differences and allow you to run your code under both Python 2 and Python 3. As a single Python file, Six can be easily copied into your project. That’s a plus. The minus is that you will need to remember to import packages from the Six.moves library since Python 3 moved several functions to different modules.

2TO3 is the best option for converting Python 2 code to Python 3 code. It reads Python 2 source code and applies a series of fixes to transform it into valid Python 3 code. The pros are that 2TO3 can be rigged to run automatically and can be extended to handle corner cases. The cons are imperfect conversion and the assumption that you want to continue coding in Python 2.

Python-Future, much like Six, provides a compatibility layer for Python 2 and 3 code, but Python-Future is intended for those that want to focus on writing Python 3 code going forward. So, unlike Six, Python-Future allows you to issue standard import commands. The con is that it assumes you only want to write Python 3 code.

A Smooth transition

Python 3 resolves issues found in the Python 2 series and brings improvements to the language. As the end of support for Python 2.7 looms, migration to Python 3 becomes a necessity. You have a number of choices for migrating from Python 2 to Python 3; the recommended course of action is to modernize incrementally in order to address failures progressively, rather than being overwhelmed by the task/errors all at once. Use the information and recommendations above to help navigate this journey.


Bart Copeland

Bart Copeland is the CEO and president of ActiveState, which is reinventing Build Engineering with an enterprise platform that lets developers build, certify and resolve any open source language for any platform and any environment. ActiveState helps enterprises scale securely with open source languages and gives developers the kinds of tools they love to use.

Bart holds a Master of Business Administration in technology management from the University of Phoenix and a mechanical engineering degree from the University of British Columbia.

Inline Feedbacks
View all comments