Skip to content

Moving from Python 2 to Python 3

Python

Long ago, I wrote an entrance related to how to install Python 2 and Python 3 on the same Windows machine. But …

Are those changes so important that both versions have compatibility problems?

The answer is yes. A change in the print statement print “hola” from Python 2.x is noew print(“Hola”) in Python 3.x. as a matter of fact, there is some incompatibilities.

In order to help you when migrating, the web python3porting.com can help you a lot, where you can read the best way to migrate to version 3.x.

You can automatize the migration thanks to 2to3, but also, there is a tool that allow to migrate code from Python 3 to Python 2, called 3to2.

If you need to know the main changes between versions 2 and 3, I encourage you to go to this notebook http://htmlpreview.github.io/?https://github.com/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.html, where you can see how to adopt to the new syntax.

Python 2.x is the favorite already

Although Python 3 is available since 2008, in this article http://alexgaynor.net/2013/dec/30/about-python-3/ you can read that even the advantages of Python 3, still Python 2 is still the most used today, and it will for a long time (unless an in-volution will occur, for joining the splitted ways of Python 2 y Python 3).

A long ago, I tried Python 3 but I uninstalled everything to focus exclusively as a Python 2.7 developer. I am noy a wierd developer because Python 2 is still the favorite!

Here you can read another article explaining why the migration to Python 3 is slow, mainly because several modules are still not ported to Python 3, and that’s a break for the migration process.

In the meantime, Python 3 is gaining popularity, as you can read on this article: http://sealedabstract.com/rants/python-3-is-fine/. Is a large one, but you can read he benefits of Python 3.

When do I move to Python 3.x?

The answer, now, is when circunstances force me, and those circunstances depends on ESRI ArcGIS, by now, using Python 2.7.x, and there is no movement to change to Python 3. Lo que sí es cierto es que procuro escribir el código para que sea ejecutable en ambas versiones sin ningun tipo de modificación, a pesar de las características de una y otra versión.

On 2014, there was a survey showing how Python 3 was gaining popularity: blog.frite-camembert.net/python-survey-2014.html.

Until you move to Python 3, read the next paragraph:

Writing code for both

With no doubt, The best thing you can do is writting code compatible with both versions, and in order to do that, I encaourage you to read this: http://python-future.org/compatible_idioms.html.

There is another good option, a book Porting to Python 3, a must-read for writting code working for both.

I must admit that during the last months, I have tried again Python 3, and because I wrote code compatible, I need almost no changes in several projects I am working on right now, so my experience with Python 3 is not as traumatic as I thought.

To end with, Python 2.7 will not be updated after 1st January 2020.

Now, are you ready to change? Happy coding

Manejando Datos Newsletter

Noticias, entradas, eventos, cursos, … News, entrances, events, courses, …


Gracias por unirte a la newsletter. Thanks for joining the newsletter.