(One)-minute geek news

Group talks, Laboratoire de Chimie, ENS de Lyon

Update your Python2 scripts


Hooray ! The new and improved Python is here ! I am not talking about the last Python release (3.8.0a4) nor the speculations about Python4.0, but about the Python3.X branch that has been released more than a decade ago ! Yet, this announcement is still relevant as many scripts are still not Python 3.X compilant (not mentionning, code being currently actively produced for Python2.X)... It is (finally) time to update your old Python2.X scripts now to enjoy long-term support and maintainability.

A bit of history/context

Python3.0 is a major Python version release that followed Python2.6. This teared the Python community: as the Python3.X branch was officially developped, the Python2.X branch was also actively developped and lead to the Python2.7 version. It was then decided that the Python2.7 version would be the last of the Python2.X branch.

Python3.0 introduced backward incompatibilities with the previous Python branch. This means that a Python2.X script might not behave correctly if executed by a Python3.X interpreter (and reciprocally).

Python3.0 has been released in 2008, yet many scripts are still found (and actively developped) in Python 2.X without a Python3.X compatible version.

Why is it so? I think this is due to 3 reasons:

  • Python2.7 will still be maintained, but only until 2020 (PEP 373). There even has been a proposal to officially bury Python2 at PyCon 2020, and countdowns can be found (https://pythonclock.org/)
  • Until Ubuntu 18.04, Python3 was not the only default version installed. Also, PEP 394 states that the python command on unix-like systems should link to Python2.7!
  • Since Python3 introduced backward incompatibilities, some Python2.7 scripts should be migrated, which would cause maintenance costs...

So unless you want your scripts to be obsolete by the end of my thesis (and I am already in second year...), you should do a migration to Python3. Let us see now a few changes that happened, so that you can code in Python3 from now on.

Upgrading your Python2.7 scripts

As you can imagine, there are many changes between Python2.6 and Python3.0 (see ). Let us review a few notorious ones here:

  • The most major change is the handling of strings. In a nutshell, now every string (str type) is Unicode-encoded and is represented by raw binary data (bytes type). bytes and str types cannot be implicitely mixed. So, unless you have specific encoding-related string manipulations in your code, you should be fine.
  • Probably the most notorious one is the print statement that became a function. So replace your:
    print my_stuff
    by
    print(my_stuff)
    and that's all.
  • A division of integers returns now a float. So
    1/2
    is evaluated as 0.5 in Python3, whereas Python2.X would evaluate it as 0 (the interger division or quotient 1//2)!
  • An integer (int type) in Python3 can be arbitrarily large! In Python2.X you would be limited to the 32/64-bit representation (±2,147,483,647 or ±9,223,372,036,854,775,807), unless you would use a long type.
  • map(), zip(), range(), dict.keys(), dict.values() dict.items(), ... return iterators/views instead of a list.

Can't there be a way to apply these changes automatically to your scripts? Well today is your lucky day, because Python2.7 actually comes with an automated Python 2 to 3 code translation tool! Simply execute 2to3 -w my_python2_script.py in your terminal and your my_python2_script.py is now Python3 compiliant! (I advise you to make a backup of your script beforehand, better safe than sorry...)