Write Python 3, while supporting Python 2.7

In this post I discuss an approach for writing code in Python 3, and still support Python 2.7. I've recently used this approach in one of my own projects. Most projects should get away with only minor modifications and an automatic translation step during package-build. However, there are some pitfalls (bytes/str) that might need special attention.

I've always been a proponent of Python 3, and I have been bashing Python 2 (a.k.a. legacy Python) at many opportunities. To be fair, Python 3 was announced just a few months after I started using Python, so I did not have much existing code that was holding me back. In fact I started writing Python 3 wherever the dependencies allowed it, and indeed the Pyzo IDE was written in Python 3 (almost) from the start. For most other projects, though, I have written code that runs on both Python 3 and Python 2.

more ...