python tab完成Mac OSX 10.7(Lion)

在升级到狮子之前,我已经通过terminal完成了在python shell中的工作。 按照这些说明 ,有可能使标签完成工作。

自从升级到Lion后,我现在无法在Python的terminal会话中完成标签页的工作。 我已经按照上面的说明去信了,但是它仍然不起作用。

Lion中的readline模块有什么不同吗? 挂钩到“选项卡:完整”选项不再似乎工作。 我想知道是否terminal是忽略readline,或者如果它是Python本身。

Python版本:2.7.1

编辑:

通过标签填写,我的意思是我可以做如下的事情:

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

苹果公司没有发布GNU readline与OS X.它确实运送BSD libedit其中包括一个readline兼容性接口。 Apple提供的系统Pythons以及python.org安装程序中的64位/ 32位Pythons都是使用libedit构build的。 问题是libedit支持的命令与readline完全不同(参见例如这里的讨论)。 传统的只有32位的python.org安装程序确实使用GNU readline就像其他一些用于OS X的Python第三方分发者一样,比如MacPorts。 有可能你以前使用过这样的Python,而不是最近的Apple。 除了修改Django之外,您还有几个选项:您可以安装第三方replacereadline模块; 或者您可以使用GNU readline附带的另一个Python。 然而,你不应该在10.7上使用python.org的32位Python,因为不幸的是,10.7上的Xcode 4不再包含gcc-4.0和OS X 10.4u SDK,这些Pythons需要用C来构build和安装软件包扩展模块。

将以下内容放入python启动文件中将启用libedit界面和典型的readline模块的标签填充。 有关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") 

由于它使用libedit / editline,启用自动完成的语法有点不同。 您可以通过键入以下命令来首先强制emacs绑定(如果没有错误,则与readline一样):

readline.parse_and_bind("bind -e")

然后你可以添加链接到你的TABbutton的自动完成(man editrc):

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

如果你想支持缩进,并有历史(在互联网上find),它应该是这样的(除非我犯了一个错误):

 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