Migrate your code to Python 3

Migrating the code in the first instance to support both Python 2 and Python3 gives an opportunity to clean up the code base as part of the migration. The suggested processed is outlined below with a commit after each stage.

Run autopep8

This is a simple cleanup before starting. This sorts out some whitespace issues, indentation, line lengths, etc.

pip install autopep8
autopep8 {{directory}} --in-place --recursive \
   --ignore E133,E226,E228 --max-line-length 110

Support Python 3

Include support for Python 3 inside your Python 2 code using a two stage process

futurize -1 -w .

This modernizes the code to Python 2.7 standard (print function, “as e” for exceptions, absolute imports).

futurize -2 -x division_safe -w .

You probably do not want to accept everything that this does so check what it does in detail. In this case git add -p is a very useful command.

Issues

  • futurize will wrap items in list() if they used to return lists but now return iterators or views. Sometimes you do not want the list e.g. instead of for key in list(data.keys()): use for key in data:. Also look at Additional Unpacking Generalizations

  • futurize will preserve old style division, so enable __future__ division and take care of using // when needed.

  • L constants cannot be used. All ints are long in Python 3. I you need a long in Python 2 use long() and from past.builtins import long

  • basestring has gone. Checking isinstance(x, basestring) requires from past.builtins import basestring as a temporary fix.

  • iterivalues and iteritems are gone. Use values and items for dict and enable efficient Python2 with from builtins import dict