Tab-completion in Python interpreter in OS X Termi

2019-01-30 08:49发布

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.

7条回答
▲ chillily
2楼-- · 2019-01-30 09:29

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.

查看更多
Viruses.
3楼-- · 2019-01-30 09:29

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.

查看更多
祖国的老花朵
4楼-- · 2019-01-30 09:33

The documented way to tell libedit (the Mac OS semi-readline) from the real one is: if "libedit" in readline.doc: pass # Mac case else: pass # GNU readline case

查看更多
贪生不怕死
5楼-- · 2019-01-30 09:42

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
查看更多
倾城 Initia
6楼-- · 2019-01-30 09:42

After crashing into many issues dealing with Python (2 and 3) on FreeBSD, I finally got a proper extension to work using libedit directly as the completer for Python.

The basic issue with libedit/readline is that Python's completion and input was heavily bent towards GNU readline... Sadly, this is actually not a particularly good interface. It requires a giant number of globals in C and does not work well on an "instance" basis.

Solution:

https://github.com/mark-nicholson/python-editline

This is a true separate python extension which directly links to libedit using the actual "libedit" interface -- not the readline glue on the side.

I have tested it pretty thoroughly on Ubuntu, FreeBSD, OpenBSD, NetBSD and MacOS -- results are posted in the readme. The c-code is very clean and has virtually no platform dependent bits -- unlike the readline.c module in Python.

Notes: It works on Python3 > 3.2. It is NOT a drop-in replacement for 'import readline' in other scripts, but those scripts can be adjusted easily. It can co-exist with readline.so -- there is code for a sitecustomize.py file which enables the selection. It can use a distribution 'libedit.so', a custom built one or libedit built into the extension itself.

查看更多
你好瞎i
7楼-- · 2019-01-30 09:47

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

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
查看更多
登录 后发表回答