Preparing for Python 3 Now

Porting an existing code base to Python 3 can seem like a huge and scary task, but there are things that can be done now, while you are still using Python 2.7, that can help alleviate the Python3 migration pain in the future.  Python provides the 2to3 tool to automatically port existing code, but in my experience with it I found that I still had to do a fair bit of by-hand editing and I ended up with 2 sets of code, one for Python 2.7 and one for 3.2+, which just adds more difficulty.

Many of the Python 3 features have been back-ported to Python 2.7 in one way or another, so the best way to reduce porting pain in the future is to write code that is at least mostly compatible with both Python 2.7 and 3.4+ at the same time.  It's not too hard to do with new modules and existing modules can usually be migrated over with a few minutes of work.  In a nutshell, changing a few things in your coding style and practices now can make the future migration easier.  Based on my experience my suggestions are:

  1. Use all the Py3 "from __future__ import ..." options.  The one that had the biggest impact for my code was absolute_import
  2. Explicitly close() all files when done with them. 
  3. Begin thinking of strings as being different types (unicode or bytes) based on their use, even if they really are the same str type in Python 2.7.  In other words, think of all text as unicode objects and all data as bytes objects, and write your code accordingly.
  4. Use bytes() instead of str() for converting to data.  It's just an alias for str now but they will need to be changed for Py3 anyway.  Continue to use str() for converting things that are meant to be readable text, not data.
  5. Use the six module (https://pypi.python.org/pypi/six) or something like it to help smooth out some of the other differences between Py2 and Py3.  For example, instead of using "isinstance(obj, basestring)" you can use "isinstance(obj, six.string_types)".  It also helps you deal with modules or classes that have changed names or locations in the standard library.  Unfortunately it's hard to remember to actually use the things in six until you are using Py3 and are getting errors.

These practices, and a few others that will help write code that is compatible with both Python 2.7 and Python 3, are also discussed in this excellent summary in the Python documentation.

Also see this Python 2-3 compatible cheat sheet, and the book "Supporting Python 3". 

 

Have more questions? Submit a request

Comments

  • Avatar
    Keith Edmonds

    What is a time frame for a Canopy version built in Python 3? It seems most of the important packages have already moved over. What about building on greater than Python 3.4 since it comes with pip? And while I am posting, add rootpy to your package list, please.

  • Avatar
    Jonathan March

    @wintersun: 1) No timeline yet. 2) VTK, hence Mayavi, have not moved. 3) Rootpy 0.5 is available to subscribers through community repo, or you can install a newer version yourself following the guidelines at https://support.enthought.com/entries/23389761-Installing-packages-into-Canopy-User-Python-from-the-OS-command-line

  • Avatar
    Jonathan Nyquist

    It's been a while, so I thought I'd ask again.  When are we likely to see a release of Canopy built on Python 3?  With Guido vowing there will never be a Python 2.8  (We are at 2.7.8, what are they going to do after 2.7.9?) and the upcoming Python 3.5 promising an infix operator for matrix multiplication, the move to Python 3 is becoming urgent. 

  • Avatar
    Nick Eubank

    Seconding what Jonathan Nyquist asked: any updates on canopy built on Python 3?

  • Avatar
    Jonathan March

    Intense work continues on the changes to Canopy that will make possible Python 3 support and many other important capabilities. No timeline to announce yet.

  • Avatar
    Michelle Farley

    Yes - having Python 3 available would be greatly appreciated.  Also - a minor thought but having up and down votes like stack overflow might help reduce the amount of comments posted. 

  • Avatar
    Jonathan March

    An interesting blog post from the PyCon 2015 Language Summit: http://lwn.net/Articles/640181/ . And it's worth noting that one of the packages that has not been migrated to Python 3 is VTK (and hence Enthought's Mayavi which is built on VTK).

  • Avatar
    David Jung

    Any update on Python 3 support in Canopy?

  • Avatar
    Jonathan March

    @DavidJ: As you probably know, VTK is now available for Python 3, which is great news. The next major release of Canopy will support multiple Python platforms, including Python 3. No ETA but not imminent.

  • Avatar
    Charles Walker

    Any update on a timescale for a Canopy release which supports Python 3?

  • Avatar
    Jonathan March

    Updated with link to excellent new article from Python.org

  • Avatar
    Michael Davidson

    Any update on the next major release of Canopy (which will support Python 3)?

  • Avatar
    Jonathan March

    Python 3.5 is supported in Canopy 2.0, to be released in the next few weeks, and is already supported in the Enthought Deployment Manager (EDM):
    Product Description https://www.enthought.com/products/edm/ 
    Documentation http://docs.enthought.com/edm
    Download https://www.enthought.com/products/edm/installers/ (6 MB installer) 
    Overview & Release Notes https://support.enthought.com/hc/en-us/articles/217372346

    Edited by Jonathan March
  • Avatar
    Vitas Anderson

    Is Mayavi available in Canopy 2.0 python 3.5?

  • Avatar
    Vitas Anderson

    Ah, I see that Canopy 2.0 isn't actually available yet :-(
    Any news on when it will be released (hopefully with Mayavi)?

  • Avatar
    Jonathan March

    @Vitas -- Canopy 2.0.0 is released (phased release, not yet on download page). Replying further on your existing support ticket.

  • Avatar
    Jonathan March

    Enthought Python 3.5 has been available for the command line via EDM for several months now. See https://support.enthought.com/hc/en-us/articles/217372346

Powered by Zendesk