OS-X terminal behaves oddly after running Python interactively

5,728

Solution 1

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

Solution 2

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

Solution 3

This worked for me (using OSX and pyenv):

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" \
PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 \
pyenv install -v 2.7.11

Source: https://medium.com/@pimterry/setting-up-pyenv-on-os-x-with-homebrew-56c7541fd331#.urbdkrc9l

Share:
5,728

Related videos on Youtube

Andrew
Author by

Andrew

Improve every piece of code you modify

Updated on September 18, 2022

Comments

  • Andrew
    Andrew over 1 year

    After running a python repl (2.7.10) in the os-x terminal and exiting, the terminal no longer echos typed input and some other strange things happen. If I run python again and exit(), then the terminal seems to be restored. I now just close the tab and open a new one, so I havent tested that extensively.

    I guess this is related to terminal settings, but its is a recent thing and I haven't changed anything that I recall. Python is installed with macports, if there is something to do with mis-matched dependencies with some console library. 2.7.10 was installed a while ago, and this didn't happen after installation.

    Cracks knuckles and opens terminal

    [~]: virtualenv-2.7 venv-test
    [~]: source venv-test/bin/activate
    [~]: python
    >>> ^D>>>       *(press ctrl-d, the 2nd >>> is strange)
    [~]:            *(type something like `ls-l`, nothing shows on console)
    [~]: -bash: ls-l: command not found`
    

    Additionally, running python again and hitting ctrl-d results in this output:

    >>> ^D           (ctrl-d, then hit enter, doesn't exit)
    >>> ^D           (...)
    >>> ^D           (...)
    >>> exit()       (typing exit() quits the session)
    >>> [~]: 
    
  • Andrew
    Andrew over 8 years
    stty echo seems to fix the problem (stackoverflow.com/questions/32475928/python3-messes-up-term‌​inal). Not sure if it takes care of it all. I think somethings a little wrong with my python, since syntax errors require me to manually hit newline to start entering a new command, and it shouldn't be that way.
  • Andrew
    Andrew over 8 years
    Though this doesn't fix the problem, it at least allows me to fix it after the fact. Thats good enough for me, for now. If there is any way to find out why this is happening, I'd love to know.
  • Andrew
    Andrew over 8 years
    Damn, thanks for finding that bug report. FWIW, It isn't possible to uninstall python27 (too many packages depend on it, normally), so you should rebuild instead. Run sudo port selfupdate && sudo port clean python27 && sudo port install python27 +readline. If your local tree is out of date, then it won't have the +readline variant available; the selfupdate will fix that.