Python 2 EOL: It's time to move on and embrace Python 3
Have you ever wondered when exactly 2020 became so complicated? Any Python developer will tell you, "on January 1."
The beginning of the year marked the Python 2 EOL (end of life), meaning no further updates will be coming in the future. In April, python.org released some final fixes, encouraging users to switch to Python 3.
In theory, Python 2 should've become a better version of itself – a stronger, faster, more up-to-date Python 3. But, is it really so in practice?
Below is a brief insight into why you do need to port the code, some technical and organizational difficulties behind migrating to Python 3, and the key ways to perform it as smoothly as possible.
Old Python versions die hard
It's not possible to make a long Python 2 EOL story short because the 3rd version was released back in 2008, and it still didn't gain as much popularity as expected. Interesting fact: the original Python 2 final was scheduled for 2015, so the development community was expected to reach the acceptance stage by now.
Of course, EOL doesn't mean that all Python 2-based applications turned into a pumpkin – they still operate, but without any official support.
However, you don't want to be unable to perform a new installation of your legacy application one day, do you?
Why port the code from Python 2 to Python 3
- No official bug fixes or security updates. Python's core developers won't perform any bug fixes or launch new security patches for Python 2. However, Python is open source, so developers can fork it.
But, if someone continues maintaining a library for 2.x, it not only holds other developers back from adopting Python 3, but it also violates the security of personal or companies' data.
- No third-party help.Many popular third-party packages and libraries hosted in the PyPI repository are gradually shifting to Python 3. Still, third parties may offer paid support for the projects on older Python versions for longer than the core community does that.
Also, note that the team of pip, the package installer for Python, doesn't provide any guarantees that it would ensure its smooth running for a long time after EOL.
- No vendor support. Python 2 is no longer the default Python version for Linux, although it's still open for installation. Red Hat will support the Python 2 package within Red Hat Enterprise Linux, with no new functionality being added to Python 2 in RHEL 7 and earlier.
There's a chance of running the containerized Python 2 version on a cloud platform, but cloud service and Docker images providers couldn't further maintain their Python 2 containers. For instance, AWS has claimed that it will issue critical security patches for the Python 2.7 runtime (but not to third-party packages) until at least the end of 2020.
As for Ubuntu and Debian, they are also actively removing Python 2 and all its dependencies.
- No talent. The longer you stay on Python 2, the harder it's getting to scour the job market for engineers willing to deal with an outdated version of the programming language.
How to migrate to Python 3
As per the JetBrains survey, 10% of respondents are still actively using Python 2 despite all persuasions.
Still, keeping software updated is one of the most important parts of the development lifecycle. For those who finally embraced a new Python norm, we've gathered some technical clues helping you port the code to Python 3.
- Enforce at least 80% test coverage. The more tests you perform, the less likely undetected bugs will break the crucial functionality during the update process. Additionally, insufficient test coverage could cause a delay in the new app version release.
- Use caniusepython3 to find which dependencies are blocking your shift to Python 3. Then, determine whether it's possible to replace these dependencies. Finally, once you've migrated to Python 3, make sure your dependencies have also been ported.
- Choose the most appropriate instrument for automated Python 2 to 3 code migration. In a nutshell, such programs read 2.x source code and apply a series of fixers to change it into proper 3.x code. The most popular tools are Futurize, Py2to3, and Six.
- If some code parts work differently on Python 3, follow the opportunity identification approach instead of the version identification when writing specific code for different Python versions.
- Bear in mind that in Python 2 str worked for both text strings and binary, but in Python 3, there's a strong distinction with str for text and bytes for binary data.
- For best practices, new Python 3 features, and more in-depth analysis, visit the official Python documentation and other useful resources.
Business of migrating to Python 3
Apart from possible technical issues, software migration poses a range of organizational challenges. The scope of work includes additional analysis to fill in knowledge gaps, determining and optimizing the cost of migration, as well as preparing employees to embrace the new system.
Also, it's hard to predict how many man-hours it'll take to deliver a new solution and whether the efforts you invest in can be balanced against the expected benefits (most probably, not).
Indeed, when your app has gained a solid reputation on the market, it becomes difficult to focus solely on migration instead of developing new features, thus disadvantaging the company in a highly competitive environment. Beyond that, it's quite common that the revenue-generating app hasn't been in the active development stage for a long time and the company lack specialists with the knowledge of a legacy system and other necessary qualifications.
That's why, for some projects, it might be worth attracting contractors or companies specialized in the application porting.
Better late than never
Whether your plans are focused on updating, self-supporting, or rebuilding the app in a different technology, it's high time to get the show on the road.
If you decided to proceed with Python 3 after all, don't fret. As Python's popularity increases and the knowledge community matures, you'll have little trouble migrating to Python 3.
iTechArt has a proven track record of Python 3 migration within both standalone tasks and the existing projects. So, if you need advice, just drop us a line.