几个月前,我写了一个博客帖子详细介绍了如何实现标准的Python解释制表完成 - 一个功能,我曾经以为只在IPython中。我发现它大大方便因为我有时不得不切换到标准的解释是由于IPython的unicode的问题。

最近我一直在做OS X的一些工作,我的不满,剧本似乎并没有为OS X的终端应用程序的工作。我希望你们中的一些与OS X的经验也许能帮助我麻烦,拍它,所以它可以在终端正常运行,也是如此。

我再现下面

的代码
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))

请注意,我稍微从版本编辑它在我的博客文章,以使IrlCompleter与真正的标签,这似乎是有什么在终端Tab键输出初始化。

有帮助吗?

解决方案

要避免使用更GPL代码,苹果不包括实际的readline。相反,它使用了BSD许可 libedit ,这是唯一的主要为readline的兼容。建立自己的Python(或使用Fink或MacPorts的),如果你想完成。

其他提示

本应在Leopard的蟒蛇工作:

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

而这一个不:

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

将它保存在〜/ .pythonrc.py和.bash_profile中执行

export PYTHONSTARTUP=$HOME/.pythonrc.py

下面是装载片完成的Windows / OS X / Linux在一次拍摄一个完整的跨平台版本:

#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")

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

这适用于我在Linux bash和OS X 10.4

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

如果在尝试上述之后,它仍然无法正常工作,然后尝试在shell执行:

sudo easy_install readline

然后,创建的〜/ .profile中与该内容文件:

export PYTHONSTARTUP=$HOME/.pythonrc.py

和一个〜/ .pythonrc.py 与该内容文件:

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

由于史蒂芬班福德以的easy_install的尖端和尼古拉斯了解文件内容。

在记录方式告诉从真实一个libedit(Mac OS的半readline的)为:   如果在readline的 “libedit” 的文档即可。:      通过#Mac机箱   其他:      通过#GNU的readline情况

撞毁处理在FreeBSD Python的(2和3)的许多问题,我终于有一个合适的扩展直接使用libedit作为完成者为Python工作。

与libedit / readline的基本问题是Python的完成和投入是对GNU的readline严重弯曲。不幸的是,这其实不是一个特别好的界面。它要求全局的巨大的数量在C和不“实例”的基础上很好地工作。

解决方案:

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

这是直接链接使用实际“libedit”接口到libedit真正单独的Python扩展 - 而不是在侧面readline的胶

我已经相当彻底的测试它在Ubuntu,FreeBSD中,OpenBSD的,NetBSD的和MacOS - 结果公布在自述。的c代码是非常干净,并且具有几乎没有依赖于平台的位 - 不象在Python

的readline.c模块。

注意:     它适用于Python3> 3.2。     这不是一个下拉更换为其他脚本“进口的readline”,但这些脚本可以很容易地进行调整。     它可以共存readline.so - 有一个sitecustomize.py文件,该文件允许选择代码。     它可以使用分配“libedit.so”,内置于扩展本身一个定制一个或libedit。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top