A nice way to judge python version by import statement: https://github.com/facebook/tornado/blob/master/tornado/web.py try: from io import BytesIO # python 3 except ImportError: from cStringIO import StringIO as BytesIO # python 2