Simple Conversions from Python 2.7 to Python 3.x

Moving from Python 2.7 to 3.x is covered in much more detail here. This list is just a list of items I’m checking as I make the switch. It’s somewhat reflective of my own coding style and is meant more as a checklist for myself than anything, but if it helps you, great!

  • Change print to print().
  • Change xrange to range.
  • Change raise, Execption "Message" to raise Exception("Message").
  • Change from module import x to from .module import x, if module is a relative import and not on the PYTHONPATH. If I’m importing the entire module, it’s now from . import module.
  • Anywhere there is integer division being used, change it from / to //.
  • If I use the result of range(...) as a list, explicitly convert it to a list, e.g., list(range(...)).