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
pythoncommand 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 (
strtype) is Unicode-encoded and is represented by raw binary data (bytestype).bytesandstrtypes 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
printstatement that became a function. So replace your:print my_stuffbyprint(my_stuff)and that's all. - A division of integers returns now a float. So 1/2is evaluated as
0.5in Python3, whereas Python2.X would evaluate it as0(the interger division or quotient1//2)! - An integer (
inttype) 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 alongtype. 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...)