简体   繁体   English

python选项卡完成Mac OSX 10.7(Lion)

[英]python tab completion Mac OSX 10.7 (Lion)

Before upgrading to lion, I had tab complete working in a python shell via terminal. 在升级到lion之前,我通过终端在python shell中完成了tab。 Following these instructions , it was possible to have tab complete working. 按照这些说明 ,可以使标签完成工作。

Since upgrading to Lion, I am now unable to get tab complete working in a terminal session of Python. 自升级到Lion以来,我现在无法在Python的终端会话中完成选项卡的完整工作。 I've followed the above instructions to the letter, and it still does not work. 我按照上面的说明写了这封信,但它仍然不起作用。

Is there a difference with the readline module in Lion? Lion中的readline模块有什么不同吗? Hooking in to the 'tab:complete' option no longer seems to work. 挂钩'tab:complete'选项似乎不再起作用。 I'm wondering if it is terminal that is ignoring readline, or if it is python itself. 我想知道它是否是忽略readline的终端,或者它是否是python本身。

Python version: 2.7.1 Python版本:2.7.1

Edit: 编辑:

By tab complete, I mean I could do something like the following: 通过选项卡完成,我的意思是我可以执行以下操作:

# django
import MyModel
MyModel.objects.a[TAB] # will complete to all()

Apple does not ship GNU readline with OS X. It does ship BSD libedit which includes a readline compatibility interface. Apple不提供OS X的GNU readline 。它提供BSD libedit ,其中包含一个readline兼容接口。 The system Pythons shipped by Apple and the 64-bit/32-bit Pythons from python.org installers are built with libedit . Apple发布的Pythons系统和python.org安装程序的64位/ 32位Pythons都是用libedit构建的。 The problem is that the commands supported by libedit are completely different from those of readline (see for example the discussion here ). 问题是libedit支持的命令与readline完全不同(参见例如此处的讨论)。 The traditional 32-bit-only python.org installers do use GNU readline as do some other 3rd-party distributors of Python for OS X, like MacPorts. 传统的32位python.org安装程序确实使用GNU readline和其他一些Python for OS X的第三方分发服务器,如MacPorts。 Chances are that you were previously using such a Python and not a recent Apple one. 您可能以前使用过这样的Python,而不是最近的Apple。 You do have a few options, besides modifying Django: you can install the third-party replacement readline module; 除了修改Django之外,你还有一些选择:你可以安装第三方替换readline模块; or you can use another Python that comes with GNU readline. 或者你可以使用GNU readline附带的另一个Python。 However, you should not use the python.org 32-bit-only Pythons on 10.7 because, unfortunately, Xcode 4 on 10.7 no longer includes gcc-4.0 and the OS X 10.4u SDK which those Pythons need to build and install packages with C extension modules. 但是,您不应该在10.7上使用python.org仅限32位的Pythons,因为不幸的是,10.7上的Xcode 4不再包含gcc-4.0和OS X 10.4u SDK,这些Pythons需要构建和安装C包扩展模块。

Putting the following in the python startup file will enable tab completion for both the libedit interface and the typical readline module. 将以下内容放在python启动文件中将为libedit接口和典型的readline模块启用选项卡完成。 For more information on the python startup file, see here 有关python启动文件的更多信息, 请参阅此处

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

As it uses libedit/editline, the syntax to enable autocompletion is a little bit different. 由于它使用libedit / editline,启用自动完成的语法有点不同。 You can first force emacs bindings (as it is with readline if I'm not wrong) by typing : 您可以通过输入以下内容来强制执行emacs绑定(如果我没有错,则使用readline)

readline.parse_and_bind("bind -e")

Then you can add autocompletion linked to your TAB button (man editrc) : 然后你可以添加链接到你的TAB按钮的自动完成(man editrc):

readline.parse_and_bind("bind '\\t' rl_complete")

And if you want to support indenting and has a history (found on internet), it should look like that (unless I made a mistake) : 如果你想支持缩进并有历史(在互联网上找到),它应该看起来像那样(除非我犯了一个错误):

import readline,rlcompleter

### Indenting
class TabCompleter(rlcompleter.Completer):
    """Completer that supports indenting"""
    def complete(self, text, state):
        if not text:
            return ('    ', None)[state]
        else:
            return rlcompleter.Completer.complete(self, text, state)

readline.set_completer(TabCompleter().complete)

### Add autocompletion
if 'libedit' in readline.__doc__:
    readline.parse_and_bind("bind -e")
    readline.parse_and_bind("bind '\t' rl_complete")
else:
    readline.parse_and_bind("tab: complete")

### Add history
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
    readline.read_history_file(histfile)
except IOError:
    pass
import atexit
atexit.register(readline.write_history_file, histfile)
del histfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM