Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab-completion in Python interpreter in OS X Terminal

Several months ago, I wrote a blog post detailing how to achieve tab-completion in the standard Python interactive interpreter--a feature I once thought only available in IPython. I've found it tremendously handy given that I sometimes have to switch to the standard interpreter due to IPython unicode issues.

Recently I've done some work in OS X. To my discontent, the script doesn't seem to work for OS X's Terminal application. I'm hoping some of you with experience in OS X might be able to help me trouble-shoot it so it can work in Terminal, as well.

I am reproducing the code below

import atexit
import os.path

try:
    import readline
except ImportError:
    pass
else:
    import rlcompleter

    class IrlCompleter(rlcompleter.Completer):
        """
        This class enables a "tab" insertion if there's no text for
        completion.

        The default "tab" is four spaces. You can initialize with '\t' as
        the tab if you wish to use a genuine tab.

        """

        def __init__(self, tab='    '):
            self.tab = tab
            rlcompleter.Completer.__init__(self)


        def complete(self, text, state):
            if text == '':
                readline.insert_text(self.tab)
                return None
            else:
                return rlcompleter.Completer.complete(self,text,state)


    #you could change this line to bind another key instead tab.
    readline.parse_and_bind('tab: complete')
    readline.set_completer(IrlCompleter('\t').complete)


# Restore our command-line history, and save it when Python exits.
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
    readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))

Note that I have slightly edited it from the version on my blog post so that the IrlCompleter is initialized with a true tab, which seems to be what is output by the Tab key in Terminal.

like image 927
gotgenes Avatar asked Mar 23 '09 21:03

gotgenes


5 Answers

This should work under Leopard's python:

import rlcompleter
import readline
readline.parse_and_bind ("bind ^I rl_complete")

Whereas this one does not:

import readline, rlcompleter
readline.parse_and_bind("tab: complete")

Save it in ~/.pythonrc.py and execute in .bash_profile

export PYTHONSTARTUP=$HOME/.pythonrc.py
like image 172
B0rG Avatar answered Oct 05 '22 19:10

B0rG


here is a full cross platform version of loading tab completion for Windows/OS X/Linux in one shot:

#Code  UUID = '9301d536-860d-11de-81c8-0023dfaa9e40'
import sys
try:
        import readline
except ImportError:
        try:
                import pyreadline as readline
        # throw open a browser if we fail both readline and pyreadline
        except ImportError:
                import webbrowser
                webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")
                # throw open a browser
        #pass
else:
        import rlcompleter
        if(sys.platform == 'darwin'):
                readline.parse_and_bind ("bind ^I rl_complete")
        else:
                readline.parse_and_bind("tab: complete")

From http://www.farmckon.net/?p=181

like image 33
FarMcKon Avatar answered Oct 05 '22 20:10

FarMcKon


To avoid having to use more GPL code, Apple doesn't include a real readline. Instead it uses the BSD-licensed libedit, which is only mostly-readline-compatible. Build your own Python (or use Fink or MacPorts) if you want completion.

like image 30
vasi Avatar answered Oct 05 '22 18:10

vasi


This works for me on both Linux bash and OS X 10.4

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
like image 33
Van Gale Avatar answered Oct 05 '22 20:10

Van Gale


If after trying the above, it still doesn't work, then try to execute in the shell:

sudo easy_install readline

Then, create ~/.profile file with the content:

export PYTHONSTARTUP=$HOME/.pythonrc.py

and a ~/.pythonrc.py file with the content:

try:
    import readline
except:
    print ("Module readline is not available.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

Thanks to Steven Bamford for the easy_install tip, and Nicolas for the file content.

like image 31
Gilad Beeri Avatar answered Oct 05 '22 19:10

Gilad Beeri