Mac – Using python screws up the shell (and/or terminal?)!

bashcommand linemacportspythonterminal

The problem

Simply opening and closing the Python sub-shell screws up my shell (or possibly the terminal?). It seems to remove the shell's ability to print any text I enter, including newlines after I enter a command.

The problem occurs for python3.4, python2.7, and python2.6, but not for python2.5.

It hasn't ever occurred before (that I've noticed), and it doesn't happen for other software packages I've used (VIM, Emacs, etc.).

I've confirmed that it breaks using Apple's standard Terminal (Version 2.2.3 [303.2]), iTerm2 (build 2.1.1), and XTerm (269).

Interestingly, it breaks under bash, but not zsh or csh.

Example behavior

To demonstrate, I do the following ($SHELL is /bin/bash):

  1. Run which python3.4.
  2. Run python3.4.
  3. Hit the Return key once, then enter exit() into the Python shell.
  4. Hit Return two more times.
  5. Enter python3.4 --version.

Here's what I see for a few of the primary versions of Python I have installed:

Python3.4 — problematic

myself@localhost:~$ which python3.4
python3.4 is /opt/local/bin/python3.4
python3.4 is /opt/local/bin/python3.4
myself@localhost:~$ python3.4
Python 3.4.3 (default, Aug 24 2015, 17:25:38)
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
>>> myself@localhost:~$ myself@localhost:~$ myself@localhost:~$ Python 3.4.3
myself@localhost:~$

Python2.7 — problematic

myself@localhost:~$ which python2.7
python2.7 is /opt/local/bin/python2.7
python2.7 is /usr/local/bin/python2.7
python2.7 is /usr/bin/python2.7
python2.7 is /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
python2.7 is /usr/bin/python2.7
python2.7 is /usr/local/bin/python2.7
python2.7 is /opt/local/bin/python2.7
myself@localhost:~$ python2.7
Python 2.7.10 (default, Aug 24 2015, 11:54:49)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
>>> myself@localhost:~$ myself@localhost:~$ myself@localhost:~$ Python 2.7.10
myself@localhost:~$

Python2.6 — problematic

myself@localhost:~$ which python2.6
python2.6 is /opt/local/bin/python2.6
python2.6 is /usr/bin/python2.6
python2.6 is /usr/bin/python2.6
python2.6 is /opt/local/bin/python2.6
myself@localhost:~$ python2.6
Python 2.6.9 (unknown, Aug 24 2015, 17:53:21)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
>>> myself@localhost:~$ myself@localhost:~$ myself@localhost:~$ Python 2.6.9
myself@localhost:~$

Python2.5 — It works!

myself@localhost:~$ which python2.5
python2.5 is /usr/bin/python2.5
python2.5 is /usr/bin/python2.5
myself@localhost:~$ python2.5
Python 2.5.6 (r256:88840, Jul 31 2011, 19:30:45)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
myself@localhost:~$
myself@localhost:~$
myself@localhost:~$ python2.5 --version
Python 2.5.6
myself@localhost:~$

Running it in zsh or csh seems to work just fine:

Zsh

myself@localhost:~$ zsh
localhost% which python3.4
/opt/local/bin/python3.4
localhost%
localhost% python3.4
Python 3.4.3 (default, Aug 24 2015, 17:25:38)
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
>>> %                                                                           
ra%
ra%
ra% python3.4 --version
Python 3.4.3
localhost% exit
myself@localhost:~$

Csh

myself@localhost:~$ csh
Running csh
[localhost:~] myself% which python3.4
/opt/local/bin/python3.4
[localhost:~] myself% python3.4
Python 3.4.3 (default, Aug 24 2015, 17:25:38)
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> exit()
>>> [localhost:~] myself%
[localhost:~] myself%
[localhost:~] myself% python3.4 --version
Python 3.4.3
[localhost:~] myself% exit
exit

My thinking so far:

The fact that the behavior seems the same for three terminals makes me think it's not a terminal issue.

The fact that it works in Zsh and Csh but not in Bash makes me think it's a shell issue.

The fact that the versions that break (3.4, 2.7, 2.6) are all installed under /opt/local/bin/ (MacPorts's usual install location), but the version that works (2.5) is installed under /usr/bin/, makes me wonder if some recent MacPorts update (pushed maybe up to 20 days before Aug. 24, 2015??) may have broken something that makes Python and Bash not work together!

Best Answer

There appears to be an issue with recent Macports builds of python see this bug

A workaround is to install pyXX-readline (py35-readline has been produced just for this bug)

Another is to install python addding the +readline variant