【问题标题】:How do I add tab completion to the Python shell?如何将制表符补全添加到 Python shell?
【发布时间】:2010-09-19 18:58:25
【问题描述】:

当使用python manage.py shell 启动一个 django 应用程序时,我得到一个 InteractiveConsole shell - 我可以使用制表符补全等。

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

当刚刚使用python 启动python 解释器时,它不提供制表符补全。

谁能告诉我 django 正在做什么来给我一个交互式控制台,或者我需要做什么才能在没有 django 应用的情况下启动一个交互式控制台?

【问题讨论】:

  • 当你输入python时,你会得到一个>>>提示,对吧?这就是交互式外壳。你有什么问题?
  • 它不提供制表符补全。这就是问题所在。
  • 请更新您的问题以指定。

标签: python shell interpreter


【解决方案1】:

我可能已经找到了办法。

创建一个文件 .pythonrc

# ~/.pythonrc
# enable syntax completion
try:
    import readline
except ImportError:
    print("Module readline not available.")
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

然后在你的 .bashrc 文件中,添加

export PYTHONSTARTUP=~/.pythonrc

这似乎行得通。

【讨论】:

  • 这适用于 IPython 和 bpython 目前不可用的 Jython。
  • 出于某种原因,上述方法在我的旧 Mac 中使用“mountain lion”对我有用,但不适用于带有“el captan”的新 Mac。在启动 python 解释器之前,我每次都需要 source ~/.bashrc 使其工作。有什么建议吗?
  • @hmi 尝试按照本页底部的建议统一您的.bashrc.bash_profilejoshstaiger.org/archives/2005/07/bash_profile_vs.html 它还提供了有关它们之间差异的信息。它可能无法解决问题,但可能会有所帮助。
  • @hmi 尝试将其放入/etc/bashrc,而不是~/.bashrc
  • 仅供参考 - 在 StackOverflow 上,我可能比其他任何人都更多次地回到这个答案,所以谢谢。
【解决方案2】:

我认为 django 做了类似https://docs.python.org/library/rlcompleter.html

如果您想拥有一个非常好的交互式解释器,请查看 IPython.

【讨论】:

    【解决方案3】:

    为了记录,这在教程中有所涉及:http://docs.python.org/tutorial/interactive.html

    【讨论】:

    • 不鼓励仅链接答案,请扩展您的答案
    【解决方案4】:

    我使用ptpython - 这是一个很棒的工具自动完成shell cmd。

    安装ptpython很简单,使用pip工具

    pip install ptpython
    

    对于 django shell,您应该像这样导入 django 环境

    import os
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")
    

    相信我,这是最适合你的方法!!!

    【讨论】:

      【解决方案5】:

      修复 Windows 10 外壳:

      pip install pyreadline3  # previously, pyreadline but that package was abandoned
      pip install ipython
      

      【讨论】:

        【解决方案6】:

        看起来 python3 开箱即用!

        【讨论】:

          【解决方案7】:

          在 Python3 中,此功能默认启用。我的系统没有安装模块readline。我在Manjaro。我在其他 linux 发行版(elementary、ubuntu、mint)上没有遇到这个选项卡完成问题。

          pip 安装模块后,在导入时,抛出以下错误-

          ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory

          为了解决这个问题,我跑了-

          cd /usr/lib ln -s libncursesw.so libncursesw.so.5

          这解决了导入错误。而且,它还带来了 python repl 中的选项卡完成,而无需创建/更改 .pythonrc.bashrc

          【讨论】:

            【解决方案8】:

            是的。它内置于 3.6。

            fernanr@gnuruwi ~ $ python3.6
            Python 3.6.3 (default, Apr 10 2019, 14:37:36)
            [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
            Type "help", "copyright", "credits" or "license" for more information.
            >>> import os
            >>> os.
            Display all 318 possibilities? (y or n)
            os.CLD_CONTINUED             os.O_RDONLY                  os.ST_NOEXEC                 os.environ                   os.getpid(                   os.readlink(                 os.spawnvpe(
            os.CLD_DUMPED                os.O_RDWR                    os.ST_NOSUID                 os.environb                  os.getppid(                  os.readv(                    os.st
            

            【讨论】:

            • 嗨,您按什么键来获得“显示所有 318 种可能性?(y 或 n)”。我试过'tab'键它没有给我你的自动完成提示。我一定错过了一些简单的东西
            【解决方案9】:

            对于旧版本 (2.x),上面的脚本就像魅力 :)

            fernanr@crsatx4 ~ $ cat .bashrc | grep -i python
            #Tab completion for python shell
            export PYTHONSTARTUP=~/.pythonrc
            fernanr@crsatx4 ~ $ . ~/.bashrc
            fernanr@crsatx4 ~ $ echo $?
            0
            fernanr@crsatx4 ~ $ python2
            Python 2.7.5 (default, Jun 11 2019, 14:33:56)
            [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
            Type "help", "copyright", "credits" or "license" for more information.
            >>> import os
            >>> os.
            Display all 249 possibilities? (y or n)
            os.EX_CANTCREAT             os.O_WRONLY                 
            

            【讨论】:

            • 你的回复好像少了"~/.pythonrc"的内容
            • @MadMike 我怀疑它打算显示在ashchristopher's answer 下方……不过,不知道为什么它是一个答案。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-16
            • 2010-10-19
            • 2017-06-16
            • 1970-01-01
            • 2012-12-23
            相关资源
            最近更新 更多