if not sys.stdout.isatty():
    oldTerm = os.environ['TERM']
os.environ['TERM'] = ''
import readline
os.environ['TERM'] = oldTerm
del oldTerm
Python readline module prints escape character during import