Writing Python 2.7 code as close as possible to Python 3.x syntax

Since Django does not yet support Python 3.x, I am using Python 2.7. However, I would like to continue exploring the new Python 3.x syntax as much as possible. This leads me to the question:

  • What is the best way to write Python 2.7 code that will be most compatible with Python 3.x?

I know that starting python -3 will be

Warn Python 3.x incompatibility that 2to3 cannot be trivially fixed.

However, I am interested in using Python 3.x syntax when using Python 2.7.

For example, it seems that I should use the following import for my code:

 from __future__ import print_function from __future__ import unicode_literals from __future__ import division from __future__ import absolute_import 

The above four __future__ import statements are required with Python 3.0, but not required in 2.7, as described in Python 2.7.3 Documentation 27.11. Definitions of future language

What else?

+47
python
May 9 '11 at 13:03
source share
7 answers

Currently, many modules are rewritten in such a way that they can be executed both in Python 2 and Python 3. This is not at all difficult, and in the future it will be very easy to simply refuse support for Python 2.

Take a look at the six module, which helps with this task, encapsulating many of the differences in a convenient way:

Six provides simple utilities to wrap the differences between Python 2 and Python 3.

His website (and, of course, in the code) lists many ways to make this possible.

+18
May 09 '11 at 13:12
source share

Put the following code in the py3k.py module and import it as follows: from py3k import * . However, you need to put it in every file, but you can even leave it there if no one else uses Python 2.x, or you can just search and replace the import string with a space, and then delete the file.

 try: from future_builtins import * except ImportError: pass try: input = raw_input range = xrange except NameError: pass 

And this is what my template file looks like:

 #!/usr/bin/env python # -*- coding: utf-8 -*- """ """ from __future__ import division, absolute_import, \ print_function, unicode_literals from utils.py3k import * # @UnusedWildImport # 
+9
Oct 06 '13 at 18:58
source share

You also need to use new exception syntaxes, i.e. more

 try: raise Exception, "Message" except Exception, e: pass 

instead, you should:

 try: raise Exception("Message") except Exception as e: pass 

Also make sure that you prefix all of your binary strings with b, i.e.:

b 'This is a binary string'

For a more complete description of this topic, see http://python3porting.com/noconv.html

+8
May 9 '11 at 15:22
source share

Many Python IDEs can be of great help here.

PyCharm , for example, can be configured to check compatibility with any range of versions,

enter image description here

and report problems at any severity level:

enter image description here

+8
Dec 23
source share
 try: input = raw_input range = xrange except NameError: pass 

Are two that spring for the mind ...

+6
May 09 '11 at 1:07 pm
source share

I suggest you try the future library . On your site:

python-future is the missing level of compatibility between Python 2 and Python 3. It allows you to use one clean Python 3.x code base to support both Python 2 and Python 3 with minimal overhead.

It provides future and past packages with backports and forwards function ports from Python 3 and 2. It also comes with futuristic and pasteurized 2to3-based custom scripts to help you easily convert Py + or Py3 code to support Python 2 and 3 in one clean Py3 codebase, modulo module.

Well-known projects using python-future for compatibility with Python 2/3 are Mezzanine and ObsPy.

+3
Nov 03 '15 at 21:32
source share

Avoing range() and zip() , using xrange() and itertools.izip() instead.

+1
May 09 '11 at 1:06 pm
source share



All Articles