方法一:
    导入新特性

    1. # Python 2.x compatible
    2. from __future__ import print_function

    方法二:
    使用类似 print 功能的函数兼容 Python2 和 Python3

    1. # Python 2.x & 3.x compatible
    2. from distutils.log import warn as printf
    3. printf('Hello World!')

    另一种方案(无缓冲的输出):

    1. # Python 2.x & 3.x compatible
    2. import sys
    3. sys.stdout.write('Hello World!\n')