Fix Python – Seeing escape characters when pressing the arrow keys in python shell

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.
But after I ssh into another machine and start python there, I get sessions like:
>>> import os
>>> ^[[A

where the last character comes from arrow-up. Or, using arrow-left:
>>> impor….