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.
几个月前,我写了一篇博客文章,详细介绍了如何在标准Python交互式解释器中实现制表完成——我曾经认为只有IPython才能实现这一功能。由于IPython unicode问题,我有时不得不切换到标准解释器,因此我发现这非常方便。
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.
最近我在OS X上做了一些工作。令我不满的是,这个脚本似乎不适合OS X的终端应用程序。我希望你们中的一些人在OS X中有经验可以帮助我解决问题,这样它也能在终端工作。
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.
注意,我在我的博客文章的版本中稍微编辑了它,使IrlCompleter用一个真正的选项卡初始化,这似乎是终端的tab键的输出。
7 个解决方案
#1
8
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.
为了避免使用更多的GPL代码,苹果没有包含真正的readline。相反,它使用的是bsd许可的libedit,这是一种只读兼容的技术。如果需要完成,构建您自己的Python(或使用Fink或MacPorts)。
#2
56
This should work under Leopard's python:
这应该适用于Leopard的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
在~ / .pythonrc保存它。并在.bash_profile中执行
export PYTHONSTARTUP=$HOME/.pythonrc.py
#3
11
here is a full cross platform version of loading tab completion for Windows/OS X/Linux in one shot:
这是一个完整的跨平台版本的加载标签完成为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")
From http://www.farmckon.net/?p=181
从http://www.farmckon.net/?p=181
#4
1
This works for me on both Linux bash and OS X 10.4
这在Linux bash和OS X 10.4上都适用
import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
#5
1
If after trying the above, it still doesn't work, then try to execute in the shell:
如果在尝试了以上的操作之后,仍然没有工作,那么尝试在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:
和~ / .pythonrc。py文件的内容:
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.
感谢Steven Bamford提供的easy_install技巧,以及Nicolas提供的文件内容。
#6
0
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
用文档化的方式告诉libedit (Mac OS半读行),真正的方法是:如果“libedit”在readline中。文档:通过# Mac案例:通过# GNU readline案例。
#7
0
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.
在FreeBSD上遇到了许多处理Python(2和3)的问题之后,我最终获得了一个适当的扩展,可以直接使用libedit作为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.
libedit/readline的基本问题是Python的完成和输入严重地倾向于GNU readline…遗憾的是,这实际上并不是一个特别好的界面。它需要C语言中大量的全局变量,并且在“实例”基础上不能很好地工作。
Solution:
解决方案:
https://github.com/mark-nicholson/python-editline
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.
这是一个真正独立的python扩展,它使用实际的“libedit”接口直接链接到libedit——而不是侧面的readline胶水。
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.
我已经在Ubuntu、FreeBSD、OpenBSD、NetBSD和MacOS上对它进行了彻底的测试——结果发布在readme中。c代码非常干净,几乎没有与平台相关的位——不像readline。在Python c模块。
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.
注意:它适用于Python3 > 3.2。它并不是替代其他脚本中的“导入readline”,但是这些脚本可以很容易地进行调整。它可以与readline共存。这是sitecustomize的代码。允许选择的py文件。它可以使用发行版的libedit。因此,自定义构建一个或libedit内置到扩展本身中。
#1
8
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.
为了避免使用更多的GPL代码,苹果没有包含真正的readline。相反,它使用的是bsd许可的libedit,这是一种只读兼容的技术。如果需要完成,构建您自己的Python(或使用Fink或MacPorts)。
#2
56
This should work under Leopard's python:
这应该适用于Leopard的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
在~ / .pythonrc保存它。并在.bash_profile中执行
export PYTHONSTARTUP=$HOME/.pythonrc.py
#3
11
here is a full cross platform version of loading tab completion for Windows/OS X/Linux in one shot:
这是一个完整的跨平台版本的加载标签完成为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")
From http://www.farmckon.net/?p=181
从http://www.farmckon.net/?p=181
#4
1
This works for me on both Linux bash and OS X 10.4
这在Linux bash和OS X 10.4上都适用
import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
#5
1
If after trying the above, it still doesn't work, then try to execute in the shell:
如果在尝试了以上的操作之后,仍然没有工作,那么尝试在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:
和~ / .pythonrc。py文件的内容:
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.
感谢Steven Bamford提供的easy_install技巧,以及Nicolas提供的文件内容。
#6
0
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
用文档化的方式告诉libedit (Mac OS半读行),真正的方法是:如果“libedit”在readline中。文档:通过# Mac案例:通过# GNU readline案例。
#7
0
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.
在FreeBSD上遇到了许多处理Python(2和3)的问题之后,我最终获得了一个适当的扩展,可以直接使用libedit作为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.
libedit/readline的基本问题是Python的完成和输入严重地倾向于GNU readline…遗憾的是,这实际上并不是一个特别好的界面。它需要C语言中大量的全局变量,并且在“实例”基础上不能很好地工作。
Solution:
解决方案:
https://github.com/mark-nicholson/python-editline
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.
这是一个真正独立的python扩展,它使用实际的“libedit”接口直接链接到libedit——而不是侧面的readline胶水。
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.
我已经在Ubuntu、FreeBSD、OpenBSD、NetBSD和MacOS上对它进行了彻底的测试——结果发布在readme中。c代码非常干净,几乎没有与平台相关的位——不像readline。在Python c模块。
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.
注意:它适用于Python3 > 3.2。它并不是替代其他脚本中的“导入readline”,但是这些脚本可以很容易地进行调整。它可以与readline共存。这是sitecustomize的代码。允许选择的py文件。它可以使用发行版的libedit。因此,自定义构建一个或libedit内置到扩展本身中。