Tab-completion in Python interpreter in OS X Terminal

你离开我真会死。 提交于 2019-11-29 20:12:18

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.

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

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

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

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!