8

Every time that I run python or python3 with an interactive console, the display of the prompt gets out of sync almost immediately after the first or second interaction:

>>> [1,2,3]
>>> [1, 2, 3]
print('hi')
>>> hi

Then, when I exit out of python, this behavior carries over to bash, with the addition that when you type, nothing appears on the screen, but it will still run. In the following example, I typed date and then history | tail -n3:

$ Thu Oct  8 07:55:47 CEST 2015
$   488  python
  489  date
  490  history | tail -n3

However, after typing reset, everything goes back to normal.

Needless to say, this behavior is very annoying. What is the cause, and how can I fix it?

I am on OS X. Using iTerm or built-in Terminal.app both give the same behavior. My python builds are from macports. This problem is not present with /usr/bin/python (python 2.7.2, included with OS X).

============================

Edit: If I use zsh, then python still has problems, but everything works fine in zsh after I exit out of python.

$TERM is xterm-256color in both bash and zsh.

reynoldsnlp
  • 1,072
  • 1
  • 18
  • 45
  • 1
    Probably your python that outputs control characters and that changes the configuration of your terminal. Might be in the python banner. You can try to `s=$(stty -g)` before launching python and `stty $s` after, to save and restore the terminal configuration. But it will not help during your python interactive session... – Renaud Pacalet Oct 08 '15 at 06:14
  • 1
    how do you exit python? – Alvaro Gutierrez Perez Oct 08 '15 at 06:36
  • 1
    @ÁlvaroGutiérrez I have tried exiting with ctrl+D and `exit()` and both yield the same result. – reynoldsnlp Oct 08 '15 at 07:16
  • What's your `$TERM` variable set to (`echo $TERM`)? – Steven Oct 15 '15 at 17:03
  • @Stebalien I added $TERM to the question. It is xterm-256color. – reynoldsnlp Oct 15 '15 at 20:21
  • Please run `sudo port selfupdate`, `sudo port upgrade outdated`, `python -V`, `python3 -V`, and then `echo $BASH_VERSION` and post the output of the last three here. If you haven't switched your default shell to MacPorts's, then follow the instructions [here](http://apple.stackexchange.com/a/70189/72652) before running these commands. – Coder-256 Oct 27 '15 at 21:17
  • @Coder256 `Python 2.7.10` `Python 3.4.3` `4.3.42(1)-release`, and behavior is the same. – reynoldsnlp Oct 30 '15 at 15:23
  • You aren't using xterm though. I assume you are setting `TERM` manually? Does this problem happen if you *don't* do that? – Etan Reisner Dec 18 '15 at 03:44
  • @EtanReisner I do not set `$TERM` in my profile. Terminal.app and iTerm.app both have settings to declare the terminal type. I have never changed that setting, so I assume that `xterm-256color` is the most popular default. I can also declare as `ansi`, `dtterm`, `nsterm`, `rxvt`, `vt52`, `vt100`, `vt102`, `xterm`, or `xterm-16color`. I tried using `ansi` and `xterm` on Terminal.app and I get the same behavior. – reynoldsnlp Dec 18 '15 at 08:20
  • `TERM` is `xterm-256color` out of the box in the built-in Terminal at least on Yosemite. – tripleee Dec 18 '15 at 16:03

3 Answers3

1

Try the stty sane command in shell.

double-beep
  • 5,031
  • 17
  • 33
  • 41
mrvol
  • 2,575
  • 18
  • 21
1

@Mark has the explanation.

As for the fix:

port selfupdate
# port install python(26|27|34|35) +readline
port install python27 +readline
dnozay
  • 23,846
  • 6
  • 82
  • 104
0

As in my answer in AskDifferent there was a bug in recent python builds under Macports when the question was written due to this Macports bug as it was not including readline (for license reasons)

This has now been fixed by making a +readline variant and making this the default

Community
  • 1
  • 1
mmmmmm
  • 32,227
  • 27
  • 88
  • 117