wiki:python2and3CompatiblityEN

Version 2 (modified by Martin Kolman, 11 years ago) (diff)

--

Making an application compatible with Python 2.5 to 3.2 at the same time

Due to the wide range of Python versions on platforms supported by modRana, the modRana Python source code needs to work on Python 2.5(Maemo 5), Python 3.2(BlackBerry 10) and all versions in between(other platforms).

The six compatibility library

Exception handling

Unicode handling

Library renaming

The print() function

In Python 3, print is no longer a statement but a function. Using:

print "Hello world"

Will result in syntax error exception.

The correct way in Python 3 is:

print("Hello world")

Fortunately, the print function can be used as far back as Python 2.5, so no version specific code is needed for this. All prints just need to be converted to using the print function. This can be easily done automatically using regular expressions or semi-automatically with the ubiquitous search and replace functionality available in most editors.