有什么方法可以在 python 中组合 readline/rlcompleter 和 InteractiveConsole?
Is there any way to combine readline/rlcompleter and InteractiveConsole in python?
我正在尝试扩展 python shell(遗憾的是我无法使用 IPython)。我希望能够同时完成关键字和解释一些自定义输入(这将无效 python)。但是我无法让 readline/rlcompleter 和 InteractiveConsole 一起工作。演示问题:
$ python -c "import code; code.InteractiveConsole().interact()"
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> import string
>>> stri
点击这里的标签没有任何作用。
$ python
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> import string
>>> stri
点击标签现在完成到 "string"。
谁能解释这是为什么,是否有解决办法?
好的 - 在 python 来源中的一些挖掘揭示了答案。问题是在 InteractiveConsole
中,名称空间设置为 __main__
以外的其他名称。但是 rlcompleter 从 builtins
和 __main__
完成。 Import string
上面导入到当前命名空间,它不是 __main__
并且不被 rlcompleter 搜索。
因此,一个解决方案是构建您自己的 rlcompleter.Completer 并将 locals() 传递给 ctor:
$ python -c "import code; code.InteractiveConsole().interact()"
Python 2.7.10 (default, Jun 1 2015, 18:05:38) [GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import readline
>>> from rlcompleter import Completer
>>> readline.parse_and_bind("tab: complete")
>>> readline.set_completer(Completer(locals()).complete)
>>> import string
>>> str
str( string
这个 post 已经有 4 年历史了,所以我要建议的解决方案当时可能还没有。但至少从 Python 3.7.7 开始,以下代码允许您使用 Python 开头的任何 readline 设置并自动添加制表符完成:
import code, readline, rlcompleter
readline.parse_and_bind('tab: complete')
InteractiveConsole(locals()).interact()
之所以有效,是因为只需要导入 readline 即可。
我正在尝试扩展 python shell(遗憾的是我无法使用 IPython)。我希望能够同时完成关键字和解释一些自定义输入(这将无效 python)。但是我无法让 readline/rlcompleter 和 InteractiveConsole 一起工作。演示问题:
$ python -c "import code; code.InteractiveConsole().interact()"
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> import string
>>> stri
点击这里的标签没有任何作用。
$ python
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
>>> import rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> import string
>>> stri
点击标签现在完成到 "string"。
谁能解释这是为什么,是否有解决办法?
好的 - 在 python 来源中的一些挖掘揭示了答案。问题是在 InteractiveConsole
中,名称空间设置为 __main__
以外的其他名称。但是 rlcompleter 从 builtins
和 __main__
完成。 Import string
上面导入到当前命名空间,它不是 __main__
并且不被 rlcompleter 搜索。
因此,一个解决方案是构建您自己的 rlcompleter.Completer 并将 locals() 传递给 ctor:
$ python -c "import code; code.InteractiveConsole().interact()"
Python 2.7.10 (default, Jun 1 2015, 18:05:38) [GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import readline
>>> from rlcompleter import Completer
>>> readline.parse_and_bind("tab: complete")
>>> readline.set_completer(Completer(locals()).complete)
>>> import string
>>> str
str( string
这个 post 已经有 4 年历史了,所以我要建议的解决方案当时可能还没有。但至少从 Python 3.7.7 开始,以下代码允许您使用 Python 开头的任何 readline 设置并自动添加制表符完成:
import code, readline, rlcompleter
readline.parse_and_bind('tab: complete')
InteractiveConsole(locals()).interact()
之所以有效,是因为只需要导入 readline 即可。