【问题标题】:How can I enable tab completion for import statements in the Python interpreter?如何在 Python 解释器中为导入语句启用制表符补全?
【发布时间】:2014-07-28 18:48:48
【问题描述】:

有没有办法为 python 中的import 语句启用制表符补全?

例子:

$ cd test/
$ ls
mytest.py
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import my<TAB>
>>> import shuti<TAB>

mytest 或任何其他模块(例如shutil)没有制表符补全。我用 python 3.4 得到了相同的结果。我使用的是 Ubuntu 14.04,我的 shell 是 bash。

请注意,制表符补全在其他 Python 解释器上下文中也有效,例如补全函数和标识符名称。

我的 .pystartup 配置如下:

import atexit
import os
import readline
import rlcompleter

readline.parse_and_bind("tab: complete")

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

【问题讨论】:

  • 我建议通过pip install bpython 尝试bpython,然后启动bpython 作为解释器包装器。它提供选项卡补全、代码提示和代码着色。
  • 你应该试试IPython shell。

标签: python tab-completion


【解决方案1】:

默认 Python 的 REPL 无法做到这一点。现在唯一的(实用的)方法是使用 IPython(== REPL 在类固醇和火箭发动机上):http://ipython.org/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2010-10-15
    • 2014-05-14
    相关资源
    最近更新 更多