Facebook EmaiInACirclel
Software Development

Upgrading Python Versions – Painless Migration to Python 3

PentaGuy
PentaGuy
Blogger

The discussion about which Python versions to use in IT projects is a really topical one and raises the interest of all who work in the field. So, you have all the reason in the world to ask yourself why you should migrate from Python 2 to 3 as a developer or IT project coordinator.

There are 2 important things to know before delving into this topic.

  1. Python has become a super popular programming language in the last few years due to its simplicity, easy readable syntax and level of community support.
  2. Americans have Googled “Python” more than Kim Kardashian. Yes. Really.

Python – “The Fastest Growing Major Programming Language”

Python has filled various niches in computer science (fields that heavily operate with data) like data science, machine learning, IoT and web development. Many developers choose Python as their first option when diving into programming and according to Stackoverflow Developer Survey 2019 it is the most wanted language for the third year in a row – meaning that developers who do not yet use it say they want to learn it. Python became the second most loved language (behind Rust) and earned a solid claim to fame as being the fastest-growing major language today.

Another reason Python has become so popular in recent years is the fact that it can be used for just about anything (enterprise and first-class projects). For example, you can build Raspberry Pi applications, scripts for desktop programs and configure servers all via Python, but it’s not limited to just those types of tasks alone!

Projects built with Python? You bet we’ve got plenty of them! Pentalog has used this technology for building different kinds of projects such as an e-Commerce platform solution and an e-learning app.

python 3

The ascending trend of Python in IT project development raises the interest for migrating to newer Python versions, such as Python 3.

 

Python 2 and 3, the Two Heads of Hydra

As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported. This means that you’ll have to find ways to get support for this series as new vulnerabilities can always pop up.

The best decision is to upgrade to Python 3 as soon as possible. Python 3 was released by its creator Guido van Rossum in 2008. 12 years later, we can say that Python 3 is an interpreted high-level programming language with some incredible features like comity new syntax, Unicode support, asyncio support, type annotations, standard library extensions and better error handling.

Python versions

Python 3.8.2 is the newest major release of the Python language in 2020 and contains many new features and optimizations. Do yourself a favor and check it out!

Friendly Reminder: Python 3 is not backward-compatible.

Python Versions – What about Python 3?

From my previous examples, I can say there are several approaches for adding Python 3 as the codebase of your project:

  • Extreme: Rewrite the entire project in Python 3
  • Sudden: Stop developing new features and start porting to Python 3
  • Gradual: Gradually add support for Python 3 & drop support for Python 2

All options have their benefits and drawbacks. In the case of a small codebase, you can easily start rewriting the whole project from scratch. When you are not constrained by deadlines and your customers don’t need new features at the moment you can concentrate your whole development cycle on porting to Python 3.

The first two approaches will be hard to implement for projects that cannot afford to halt the development cycle. To alleviate any concerns, we have the 3rd option.

The practical approach for a gradual upgrade to Python’s third version contain following steps that will guarantee a smooth transition:

  1. Having good testing coverage
  2. Dropping support of older versions of Python 2 (move to Python 2.7)
  3. Write code supported by both versions
  4. Rely on your tests in order to make code work again
  5. Remove uses of deprecated libraries and syntax
  6. Start using Python 3 in production
  7. Remove Python 2 compatibility code

The most challenging part of the transition might be writing code that works on both Python versions. But, fear not! The migration process to Python 3 can be semi-automated. The Python community offers a set of tools we will examine further that will make transition smoother.

How to Update Python Versions

1. Consider Compatibility Layers

A naive approach for keeping both versions working would be to try to use first libraries from Python 3 in a try/catch statement.

try:
from
urllib.request import urlopen
except ImportError:
 from urllib2 import urlopen

r = urlopen(‘https://www.google.com‘)

However, there is a better approach to escape try / catch hell. Libraries like six and futurize provide an elegant way for adding compatibility layers for your codebase. The code below shows an elegant example of future library usage.

from future.standard_library import install_aliases
install_aliases()
from
urllib.request import urlopen
r = urlopen(‘https://www.google.com‘)

Future library provides an amazing way to write compatibility layer code, since it has a smooth method called install_aliases, you can still use Python 2, but use the Python 3 version. Furthermore, when you’re done with the transition, just delete the import of the future library and you’re done.

2. Consider Automated Tools

The example above with using compatibility layers provides a handy solution for writing code that is compatible with both versions of Python. But, the Python community has offered something better: libraries such as modernize and futurize provide automated command line tools for code conversion.

Those libraries can easily convert files with Python 2 source code to Python 3 compatible. But, as a rule of thumb it’s better to double check those converted files as even machines can have bugs. A better approach for using automated conversion tools is to rely on your tests and rewrite code in case of errors.

3. Consider Package Transitions

Another issue that you should address while moving to Python 3 is third party packages and libraries. Good news! Tools like caniusepython3 allow you to check if a certain package is Python 3 compatible or not. The Caniusepython3 package provides command line utility that can check your requirements files. Caniusepython3 also provides web interface to check your dependencies.

Just Do It!

To migrate or not migrate? – this is the question that can confound many developers. The upgrading process can be hard and it does require time and money.

The Python 2 clock stopped. It is hard to predict what will happen with the projects that choose not to adopt Python 3, but obviously projects that will not adopt transition will be harder to develop, maintain and support. Working with an older version of Python can bury your projects.

I hope the above advice is helpful so you understand that Python 3 may finally have the upper hand. Since ActivePython3 is being heavily downloaded, the shift to using Python 3 is more than obvious for both developers and companies that use this technology in their projects.

 

  • Interested in testing your Python 3 knowledge? Take these quizzes by SkillValue and obtain a LinkedIn certificate that will boost your ranking as a Python specialist.

Leave a Reply

Your email address will not be published. Required fields are marked *