-1

I was using the below pystartup script in linux to make history and autocompletion on tab available in REPL Python. I have switched to OSX and I have modified the script such that tab completion works. But I am not able to figure out how to make the search work? Search couple of SO questions like Python interactive mode history and arrow keys

But I don't want to uninstall the python version that comes with OSX as it may lead to some other dependency breakages.

My python version is 2.7.10

Script

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

if 'libedit' in readline.__doc__:
   readline.parse_and_bind("bind ^I rl_complete")
else:
   readline.parse_and_bind("tab: complete")

readline.set_history_length(1000)
atexit.register(readline.write_history_file, historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
Knight71
  • 2,927
  • 5
  • 37
  • 63
  • 1
    The script works with the system python on High Sierra 10.13.4; I can also confirm it is built with readline support: `$ python -c "import readline; print(readline.__file__)"` returns `/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so`, `$ python -c "import readline; print(readline.__file__)" | xargs otool -L` shows that it is linked against `libedit` and `libncurses`. What MacOS version do you have? – hoefling May 13 '18 at 09:30
  • Also, where do you put the script - is it the correct path? Mine is `$ python -c "import site; print(site.getusersitepackages())" | xargs -I {} ls {}/sitecustomize.py`, which returns `/Users/hoefling/Library/Python/2.7/lib/python/site-packages/sitecustomize.py` on my machine. – hoefling May 13 '18 at 09:31
  • BTW, you won't be able to uninstall system python completely anyway, at least not without turning SIP off, so I wouldn't care about that. – hoefling May 13 '18 at 09:34
  • I have high sierra. – Knight71 May 14 '18 at 12:53
  • After the answer given by Tarun, I was able to get the history search. The problem was with virtualenv which was pointing to system python. – Knight71 May 14 '18 at 12:54

1 Answers1

1

You don't need to uninstall Mac OSX python. You should just install new python that can co-exist with system python.

Use below to install Python 2.7.X

brew install python@2

Use below to install Python 3.6.X

brew install python

If you don't have brew then setup the same following steps mentioned on

https://brew.sh

Once you do that it will automatically work

Readline default

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265