OS-X terminal behaves oddly after running Python interactively

There is an open bug in MacPorts on this issue:

https://trac.macports.org/ticket/48807

It says that installing python with the "+readline" option fixes the problem, which I verified by doing it myself.

sudo port uninstall python27
sudo port install python27 +readline

The terminal modes are set by your application to raw or cbreak, and when exiting abnormally the modes are not restored. In particular, the feature that maps the Enter key (control/M aka carriage return) to newline (control/J aka line feed) is often disabled in this case. You can fix the problem with returns by entering

stty sane

followed by controlJ

That will at least let you enter commands again, though the editing characters such as backspace may not be assigned to their usual functions.

Some may also advise using the reset command, but the given symptoms are not relevant to what that program does.

For further reading

  • Salvage a borked terminal
  • stty(1), interestingly though OSX is "BSD", its stty manual page is GNU
  • reset(1), from ncurses