top band

Friday 1:55 p.m.–2:25 p.m.

How to make your code Python 2/3 compatible

Brett Cannon

Audience level:
Intermediate
Category:
Best Practices & Patterns

Description

You know Python 3 is an improvement over Python 2 and you want to use it. Unfortunately you have legacy Python 2 source code that needs to stay compatible. But don't fret! This talk will show you that you can make your code be Python 2/3 source-compatible using various tools to pick up the nitty-gritty work and help modernize your Python code to newer Python 2 practices.

Abstract

Porting Python 2 code to work with Python 2 & 3 without a constant 2to3 translation step is not hard anymore. With tools such as [python-modernize](https://github.com/python-modernize/python-modernize) or [futurize](http://python-future.org/automatic_conversion.html), the mundane details are taken care of for you (e.g. syntactic changes). That leaves only high-level API choices as the roadblock to porting and a couple of gotchas to look out for (e.g. what APIs should accept bytes vs. strings). And with 2to3 out of the picture it makes development much faster. Toss in linting tools such as [Pylint](http://pylint.org/) and you can make sure that once you make your changes that you don't accidentally regress. Add constant integration testing through tools like [Tox](https://testrun.org/tox/latest/) once you can use Python 3 and your Python 3 support will become negligible to maintain. There is even tools now like [caniusepython3](https://caniusepython3.com/) which help let you know when your project's dependencies have made the switch.
bottom band background