[Tutor] combining Python 2.x and Python 3

wesley chun wescpy at gmail.com
Tue Mar 17 20:54:27 CET 2009


> 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. 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.

however, Python 2.6 (and the rest of the 2.x series) have some of
3.0's features backported to it so that you can start writing against
3.x interpreters, and i would recommend that you start there.

finally, 2.6 has a -3 switch that you can turn on to warn you about
Python 3 incompatibilities.

hope this helps!
-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list