Six is a Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions. Six supports every Python version since 2.5. 0
The name, “six”, comes from the fact that 2*3 equals 6. Why not addition? Multiplication is more powerful, and, anyway, “five” has already been snatched away by the Zope Five project. 1
So, if there is a compatability problem, you must use try-except import style to solve it:
1
2
3
4
try:
from io import StringIO
except ImportError:
from cStringIO import StringIO
Or you can get help form the amazing module six to do the dirty job for you:
1
from six.moves import cStringIO
To keep the code consistent
1
from six.moves import cStringIO as StringIO
Personally, I think it doesn’t provide so much conveniency, because we still need to worry about which module has version problem. However, it simplifies the ugly import code and provides a standard way to solve such kind of problem, why don’t use it.
There are several major problem you will meet for python version problem, you can read six documents or Reorganizations and renamings for more detail, here I just list the usage in OpenStack (as much as I’ve already come across)
Renamed modules and attributes compatibility
six, python2, python3, gerrit review
- range, xrange, range
- cStringIO, cStringIO.StringIO, io.StringIO, https://review.openstack.org/#/c/41615/
License: (CC 3.0) BY-NC-SA