[Tutor] combining Python 2.x and Python 3

Kent Johnson kent37 at tds.net
Tue Mar 17 21:44:57 CET 2009


On Tue, Mar 17, 2009 at 3:54 PM, wesley chun <wescpy at gmail.com> wrote:
>> I would like to change it so that it can be run under both Python 3 and Python 2.x.
>
> everyone who has responded so far are telling about converting your
> piece of code from Python 2 to Python 3, for example, using the "2to3"
> tool that comes with Python 2.6+:
> http://docs.python.org/library/2to3.html
>
> however, that means that you have to maintain 2 different files, one
> for Python 3 and one for Python 2.

Not necessarily. I think the intent of 2to3 is that you maintain the
Python 2.6 version and automatically create the Python 3 version from
it. So there is only one source file. At least one person found this
practical, for a large codebase (well, he says it "mostly works"):
http://wiki.python.org/moin/PortingDjangoTo3k

> no one has addressed your original
> request directly, so i'll do it here: it is nearly impossible to have
> a sufficiently complex single .py source file that will run under both
> Python 3 and Python 2 -- one single print statement would ruin it, if
> you know what i mean.

Here are three success stories of creating a single version of a
sizable project that runs in both 2.x and 3.0:
http://pythonology.blogspot.com/2009/02/making-code-run-on-python-20-through-30.html
http://mail.mems-exchange.org/durusmail/qp/441/ and followup

The postings have a fair amount of detail. Durus, QP, QPy and evoque
are all open source so you can see how they did it.
http://www.mems-exchange.org/software/ - the latest releases include
the Py 3.0 code though the README files don't mention it.
http://evoque.gizmojo.org/

Kent


More information about the Tutor mailing list