如何将标签完成添加到Python shell?

ash*_*her 112 python shell interpreter

当使用启动django应用程序时python manage.py shell,我得到一个InteractiveConsole shell - 我可以使用Tab完成等.

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)
Run Code Online (Sandbox Code Playgroud)

刚刚启动python解释器时python,它不提供制表符完成.

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

ash*_*her 196

我可能已经找到了办法.

创建一个.pythonrc文件

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

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

export PYTHONSTARTUP=~/.pythonrc
Run Code Online (Sandbox Code Playgroud)

这似乎有效.

  • 这适用于Jython,其中IPython和bpython目前不可用. (2认同)

Pet*_*ann 34

我认为django做的事情就像https://docs.python.org/library/rlcompleter.html

如果你想拥有一个非常好的交互式翻译,请查看 http://ipython.scipy.org/.

  • 我首先推荐[bpython](https://bitbucket.org/bobf/bpython). (8认同)

Tho*_*ers 26

有关记录,请参阅教程:http://docs.python.org/tutorial/interactive.html

  • 不鼓励仅链接答案,请扩大您的答案 (5认同)

小智 9

我用ptpython. https://github.com/jonathanslenders/ptpython/

ptpython是一个很棒的工具autocomplete shell cmd.安装ptpython非常简单,使用pip工具

pip install ptpython

对于django shell,你应该导入django env,就像这样

进口口

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

相信我,这是最好的方式!


小智 5

针对 Windows 10 shell 的修复:

  • pip install pyreadline
  • pip install ipython[壳]