The sunset date of Python 2 is fast approaching; as of Jan. 1, 2020, Python 2 will no longer be supported. But since Python 3 is the best path forward, companies need to consider migrating to Python 3 sooner rather than later. How can developers successfully prepare for this transition?
Evolving from Python 2 to Python 3
Millions of developers around the world leverage Python’s flexibility and ease of use to build software that ranges from powerful web applications to complex machine learning (ML)-driven software, making it one of the most popular programming languages in the world.
However, Python 2 has two functional shortcomings. For one thing, Python 2 introduced two text types that were prone to misuse. Because Python 2’s string type doesn’t clearly separate textual data from binary data, erroneous bugs often were introduced into the code. In fact, a recent report ranked Python among the least secure programming languages.
The other way it falls short is that it doesn’t provide support for Unicode because it was created before the Unicode standard was finalized. As a result, developers must manually deal with different language encodings, which is prone to error.
By basing the default core string type on Unicode, Python 3 resolves the two main problems with Python 2. This is a big gain for Python programmers because it enables multilingual programming and eliminates text/binary. The Python 3 series also gives programmers and devs other wins, like function annotations and range memory objects, which are significantly more memory-efficient. But Python 3 also introduces a number of backward-incompatible changes, including:
- Changing integer division to automatically generate a floating-point result
- Removal of classic Python 2 classes — for example, “range()” in Python 3 returns a memory-efficient iterable, not a list like Python2 offers.
- Converting the print statement to a function
Why Migrate Now?
After Jan. 1, 2020, Python 2.7 — the last version of Python 2 — will no longer be actively maintained. Enterprises with applications running under any version of Python 2 should migrate — and the sooner the better. Otherwise, developer teams will be tasked with maintaining their existing applications, which will put a significant tax on developer productivity and weaken enterprise security. Or they’ll need to turn to extended support from a commercial vendor.
Because migration to a new version of Python is significant, it shouldn’t be rushed.
Rather, migrating before the deadline will help developers take the time needed to do it well, and account for any unforeseen glitches well in advance.
Not convinced? Consider these aspects:
- Easier porting: In September 2015, Python 3.5 introduced changes that simplified code porting.
- Greater compatibility: More than 90% of the top 360 most-downloaded packages already offer Python 3 support.
- Widely supported: Python 3 is fully supported by the three largest public cloud providers. Many versions of Linux now install Python 3 by default.
How to Upgrade 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.
Before migrating, ensure your team has a good understanding of Python 3 conventions.
Next, its time to pick your tooling. There are currently three recommended packages: Six, 2T03 and Python-Future.
Goal: 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.
Goal: Convert Python 2 code to Python 3 code.
2T03 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.
Goal: Coding with Python 3
Similar to Six, Python-Future offers a compatibility layer for Python 2 and 3 code, but Python-Future is intended for those who 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.
Change Begins Now
2020 will be upon us in a few short months, making migration to Python 3 a necessity. Organizations that prepare now will not rush this important transition but give their development teams time to do it incrementally and well. This way, they can address failures as they arise rather than all at once, which would overwhelm them and possibly create issues with business continuity. The recommendations above will set you on your way toward life with Python 3.
Feature image via Pixabay.