11

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)
>>> [~]: 
  • Why are you installing Python via macports? Python v2.7.10 is included with El Cap, and I think Yosemite... Is there something in particular that you are getting from Mac Ports? – Benjamin Schollnick Oct 8 '15 at 13:01
  • Well, using 2.7.anything greater than what was shipped with mac. Not touching the system python (standard problem on unixes), running 2.7 with custom GIS options compiled in, with package mgmt for those, as well as 3.4 at the same time. But the real reason you should do it is to not fsck with the system python. – Andrew Backer Oct 8 '15 at 13:09
  • Is the python application doing any sort of window management? (eg. Using Curses, NCurses, etc) – Benjamin Schollnick Oct 8 '15 at 13:13
  • There is no application, just typing python and then ctrl-d – Andrew Backer Oct 8 '15 at 13:14
12

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
  • 9
    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. – Andrew Backer Nov 16 '15 at 3:45
8

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

  • stty echo seems to fix the problem (stackoverflow.com/questions/32475928/python3-messes-up-terminal). 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 Backer Oct 8 '15 at 10:20
  • 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 Backer Oct 10 '15 at 13:05
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.