Using and supporting Python 3

Python 3 has been supported in Sympathy for Data since version 1.4.0, but starting from 1.6.0 it is the only supported version of Python. This means that Python 2 is no longer supported, so if you really need Python 2 support you should stay on an earlier version of Sympathy.

Python 3 is gaining more and more popularity and there are good reasons for adapting to Python 3 in new projects. Popular third party libraries have matured and work well in Python 3. Some newly developed packages may not support Python 2 at all.

See Python 2 or Python 3 for more reasons why you should use Python 3.

Differences between Python 3 and Python 2

The major difference between Python 2 and 3 is its handling of unicode (called str in Python 3). In Python 3 you normally work with unicode for any kind of text unless dealing with raw binary data.

Filenames are unicode in many cases where they were bytes (typically called str in 2) in Python 2. Some examples:

__file__
sys.path
os.environ

To get the corresponding unicode in Python 2:

fs_encoding = sys.getfilesystemencoding()
__file__.decode(fs_encoding)

Dictionary methods, that is keys, values, and items, return view objects which can be iterated but not indexed.

Some modules, functions, and classes in the standard library have been renamed, for example:

# Python 2
import Queue
import StringIO.StringIO
import os
os.getcwdu()
# Python 3
import queue
import io.StringIO
import os
os.getcwd()

Python 2 and Python 3 compatibility

It is possible to write code that will run both Python 2 and 3. While you need to support users on 2 this could be a good way to move forward.

See Porting to Python 3 for more information.

Future imports

To make Python 2 behave somewhat more like 3 you can use imports from the __future__ module.

from __future__ import print_function, division, unicode_literals, absolute_import

These need to appear at the top of your module. Please read up on the details of what each of these do before you use them.

See Future module for details.

IO module

The IO module contains a backport of the open(filename, …) function from Python 3. Use io.open when working with text files. See IO module for details.

Six package

Six is a third party package for writing code that is compatible with both Python 2 and 3. It provides alternate names for the string types that can be used in place of string, unicode, and bytes. six.text_type returns unicode in Python 2 and str in 3 and six.binary_type returns str in Python 2 and bytes in 3. Alternate names for functions that have been renamed are available under six.moves. See Six package for details.

String literals

String literals are unicode in Python 3 and normally binary in Python 2, unless from __future__ import unicode_literals is used. To force the type for string literals in both Python versions, use:

b'hello' # Binary literal
u'hello' # Unicode literal

Python 3 only

If you do not need support for Python 2 there are many improvements that you will benefit from if you start with 3:

  • Better unicode handling as mentioned.
  • Many of the more advanced modules in the standard library have also been improved: subprocess, importlib, and asyncio (not available in Python 2) to name a few.
  • Better handling of import loops and no need to repeat yourself when using super().