Richard Jones' Log: On packages supporting Python 2 and 3

Fri, 26 Mar 2010

My html.py package (whose syntax "really could not be any better" in the words of one user) now supports unicode under Python 2. To achieve this I now need to produce a translated version of the module for Python 3 compatibility (unicode strings are u'text' in Python 2 which is a syntax error in Python 3).

So I'd heard that distutils somehow automagically supported running lib2to3 for Python 3. That's close to the truth. The distribute package supports installing to both Python 2 and Python 3 using lib2to3 to create the Python 3 version.

The trick is to use "from setuptools import setup" instead of "from distutils.core import setup" and then include "use_2to3 = True" in your setup() arguments. Well, that's most of the trick. As it turns out I also had to:

  1. Add "setuptools.use_2to3_on_doctests = False" because my docstrings have "print" statements in them (they're not actually doctests but the 2to3bot doesn't know that). Without that variable being set I would get a strange error deep in the 2to3 code.
  2. Run "python3 setup.py build install" otherwise the Python 2 code would still be installed. It seems "build" isn't actually run for a simple Python-only install. Took me a while to figure that one out.

The upshot is it works in the end and I hope that maybe this post can help others.