Expertise Technology

Updating Python 2 to Python 3 | Painless Migration Guide

Lisa Plitnichenko, Marketing Manager

Porting Python 2.7 Code to Python 3.X 

In December 2008, Python software foundation released version 3.0 introducing big changes to improve Python. As many people after the release did not want to upgrade their projects, the developers had been supporting both versions for some time not to hurt users’ feelings.

Although much time is given for code porting, many haven’t done it yet. Meanwhile, supporting two versions consumes much time & effort, preventing the developers from further Python 3.0 improvement.

That’s why the Python foundation decided to announce:

Users are urged to migrate to a supported version of Python 3 in order to benefit from its many improvements, as well as to avoid potential security vulnerabilities in Python 2.x after April 2020.


The biggest challenge impeding from almost effortless migration to Python 3.0 is a backward incompatibility with version 2.0. It means that the code Python 2.x will always return errors while running on Python 3.0. The dynamic Python standardization makes it difficult to mechanically migrate from Python 2 to Python 3.

This is where the help of the professional Python developers & software architects could be required.

Contents:

Differences between Python 2 and Python 3

According to the Google Trends data, the people’s interest in Python 3.0 has been continuously growing to surpass its earlier version: 

Python 2 vs Python 3 Google Trends
Python 2 vs Python 3 in Google Trends

But what features did the community add to Python 3.0 to make such a big difference?

Python 2Python 3
string typeASCIIUnicode
error handlingexcept NameError, errexcept NameError as err
printprint function is treated more as a statementprint() function, treated as a function
integer divisionnumbers without any digits are integers (3/2 = 1)intuitive way of treating numbers: 3/2 = 1.5
integer sizelimited to 32 bitsunlimited
rangexrange methodrange method
rules of ordering comparisonscomplexsimplified
leak of variablesnever changeschanges while using it inside for-loop

A few best new features in Python 3:

  1. native support for asynchronous I/O (asyncio);
  2. new MRO;
  3. f strings syntax;
  4. @dataclasses;
  5. new packaging; 
  6. assignment expressions;
  7. type annotations;

and many other awesome things! 

“Hello World”

Python 2

def main():
  print "Hello World!"

if __name__== "__main__":
  main()

Python 3

def main():
  print("Hello World!")
  
if __name__== "__main__":
  main()

Reasons for Code Porting

Who should consider the migration?

Application owners with a clear vision of future product development.

The majority of applications with a reasonable projection for the future have been already ported to Python 3.X. If you’re planning to scale your app or add new features over time, you should definitely choose the Python 3.X language versions, as unsupported code could lead to serious security gaps.

Here are more reasons why Python 3 is worth being migrated to:

  •  Large community support
Python 2 vs Python 3 on Stack Overflow statistics
Python 2 vs Python 3 on Stack Overflow

So it’s much quicker & easier to fix code bugs or get help from the community.

  • Standard library improvements are available by default.
  • Simplified process of detecting and fixing the bugs.
  • Support of modern technologies: AI, big data, machine learning.

What will happen if I do not port the code?

If you or your vendor find a security problem in Python 2, nobody from the volunteers will help fix it, as this version is no longer supported. Except for the security issues, you won’t be able to use a set of good packages built for Python 3.  

However, not every Python application should be upgraded. As for closed and self-sufficient systems without a scaling up intention, porting the code could have rather negative than the positive financial outcome, as you’ll spend the resources on doing the job that won’t be of much benefit.

How to Port Python 2 to Python 3

What should be done when upgrading Python version:

  1. Choose the strategy. Depending on your objectives, your vendor can help you pick up a custom strategy fitting for your project. Among the most common ones, it could be code rewriting, ending up with Python 2.0 version support and supporting Python 3.0 instead, or porting the project to Python 3.0 without adding new Python 2.0 features. 
  2. Understand the key difference between Python 2 and 3. You can use the above-mentioned table or go to one of the posts highlighting the difference. Being aware of the core changes in version 3.0 will tell you the areas to focus on. 
  3. Audit the code. Any upgrade, modernization, or improvement is impossible without the code audit, as it helps the developers define the possible problems, bottlenecks, and re-build the system’s architecture to enhance the performance if required. 
  4. Ensure good test coverage. In a perfect world, every major step of code porting should be accompanied by unit tests to ensure the code complies with all its functions. Good test coverage allows detecting the problems directly on the stage of development that makes it easier to fix the bugs.
  5. Use CI/CD concepts. Iterate releases ensured by the concepts of continuous integration & delivery help deliver a better code making the whole application work better. 
  6. Start using Python 3 in production.
  7. Remove Python 2 compatibility code. 

Want to learn more about updating your project? Get a free consultation:




Examples of Large Companies

If your digital product is aimed at advancing with the times, code porting is a mandatory option without which the further development of your project is under the question.

May the examples of the applications-giants dispel any of your doubts:

Instagram

The large-scale application with more than 1 billion users a month has gone through the upgrade. It takes 10 months for the technical team to port the app from Python 2.7 to Python 3, starting from removing the third-party packages that Python 3 doesn’t support, and unit testing to the smooth product rollout.

Facebook

In 2011, Jason Fried and a couple of Facebook engineers were struggling to make Python 3 the dominant version at Facebook. Although, it “was going to take some time and much diplomacy”, Fried and his team reached the result. For now, it’s embarrassing at Facebook to run only Python 2 code. 

Watch his YouTube video to get the details:

Subscribe to our newsletter

No spam, only hot&fresh posts from Jellyfish.tech team

Every month our subscribers get awesome updates

Bottom Line

The set of actions to port Python code from 2.0 to 3.0 is similar to the process of legacy modernization. The development team should proceed with caution according to a comprehensive plan and estimate the risks properly. 

Porting the Python code is still a challenge impossible without a dedicated team of developers but the outcome is worth it in most cases. 

Considering Python migration? It’s better to check with a technical partner having similar cases to decide on the value it brings (or doesn’t bring) to your business before taking steps towards updating your version of Python.

2 thoughts on “Updating Python 2 to Python 3 | Painless Migration Guide

  1. Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.

Leave a Reply

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