< Back

Everything You Need To Know About Migrating to Python 3

0

Posted by Binh Pham on 08 Nov 2019 at 08:34

Python 2 is going to hit its official end of life in 2020. “We have decided that January 1st, 2020, will be the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can.” - official statement from Python. With just 2 months to go until support ends for Python 2, companies should consider migrating their system from Python 2 to 3 without waiting any longer.

 

 

Python 3 was released at the end of 2008, 9 years after the inception of the language by its creator, Guido Van Rossum. The introduction of Python 3 has modernized the language many upgrades and new features implemented but also broke the compatibility with any earlier versions of Python. Python 3 has long been considered The Future of Python with many benefits: 

  • Cleaner code. The structure of Python 3 has been modified so that developers can perform an action with fewer lines of code required.
  • Less maintenance. Python 3 requires less effort to write and maintain code compared to Python 2. This allows engineers to focus on improving products’ live. The structure of Python 3 also allows for more efficient scalability.
  • Faster runtime. Each newer version of Python is upgraded to have faster runtime.
  • Greater community support. Python 3 is fully supported by the three largest public cloud providers. Many versions of Linux now install Python 3 by default.

There are still some companies using Python 2 for legacy purposes but more and more are switching to Python 3 because of the mentioned benefits and newly implemented features. Instagram and Facebook are two big companies that have fully migrated their system from Python 2 to Python 3. A large number of Python engineers are now using Python 3 and beginners also start with Python 3. JetBrains findings indicate 84% of programmers have made the switch. 

 

According to Lien Dang, Project Director at East Agile with 5 years of experience working with Python, "With the end of Python 2’s support, the need to migrate to Python 3 is a necessity. The process might be significant and take time, therefore, the sooner clients start to implement this, the better.” The Python Software Foundation has provided a comprehensive guide on how to port Python 2 code to Python 3 and below is a summary of its advice to make this process less painful: 

  1. Drop support for Python 2.6 and older. You should aim for only supporting 2.7 because it is much easier to work with this version. Also, Python 2.6 is no longer freely supported and thus is not receiving updates.
  2. Specify the proper version support in setup.py file ideally with the version you do support and you should at least have Programming Language :: Python :: 2 :: Only specified.
  3. Have a good test coverage, ideally, above 80%. If you don’t already have a tool to measure test coverage then coverage.py is recommended.
  4. Know the differences between Python 2 and Python 3. Python 2 is legacy, Python 3 is The Future. Some differences include Function print, Division of Integers, Unicode support, Syntax, etc. You can learn more about the differences between Python 2 and Python 3 at the “What’s New” doc for each release of Python 3 and the “Porting to Python 3” book.
  5. Upgrade your code. Modernize and Futurize are two tools that help to update your code to run under Python 3 while staying compatible with the version of Python 2 that you started. Each tool will support with different functions, so, make sure to read the relevant document for the tool you choose to see how it can help you in your situation.
  6. Beware of the integer division. You should add from __future__ import division to any and all files which use the / and // operators or to be running the interpreter with the -Q flag.
  7. Be careful with text versus binary data. Python 3 made text and binary data distinct types that cannot be mixed together. There are several steps you need to follow to make your code compliant:
    • Decide which APIs take text and which take binary.
    • Make sure you know whether the string literals in your code represent text or binary data.
    • Decode binary data to text as soon as possible, encode text as binary data as late as possible.
    • You must make a decision of whether a file will be used for binary access or textual access. You should also use io.open() for opening files instead of the built-in open() function.
    • Be careful when indexing into binary data.
  8. Use feature detection. This helps to prevent any potential problems of getting the version detection wrong and ensure your future compatibility.
  9. Make sure your code doesn’t regress by adding at least the following block of code at the top of any new modules you create:
    • from __future__ import absolute_import
    • from __future__ import division
    • from __future__ import print_function
  10. Check your dependencies. Besides porting you codes, you should also keep an eye on your dependencies, whether they have also been ported. You can use caniusepython3 to help you determine which projects are blocking you from supporting Python 3.
  11. Update the classifiers. You should update the classifiers in your setup.py to contain Programming Language :: Python :: 3. Also, add classifiers for each major/minor version your latest Python version.
  12. Use continuous integration. Make sure your codes work well under both Python 2 & 3. One tool allows you to run tests under multiple Python interpreters is tox.
  13. Optional static type checker is recommended. Static type checker like mypy or pytype can assist to port your code and also check certain functions as expected in both versions of Python.

We can clearly see that there would be a lot of work needs to be done to complete migrating Python 3 in the few short months. Organizations that prepare now will not rush this important transition, otherwise, give their development teams enough time to do it incrementally and smoothly. This way, they can address failures as they arise rather than rushing and possibly creating issues with business continuity.
 

If you are considering to migrate your system to Python 3, contact us for more information. If you have any questions regarding this, do not hesitate to contact us and we’ll quickly get back to you.

 
0 comments
Leave a comment