如何将制表符完成添加到Python Shell?

问题:如何将制表符完成添加到Python Shell?

使用启动django应用程序时python manage.py shell,我得到一个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应用程序的交互式控制台?

When starting a django application using python manage.py shell, I get an InteractiveConsole shell – I can use tab completion, etc.

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)

When just starting a python interpreter using python, it doesn’t offer tab completion.

Can someone tell me what django is doing to give me an interactive console, or what I need to do to start an interactive console without a django app?


回答 0

我可能已经找到了一种方法。

创建一个文件.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

这似乎有效。

I may have found a way to do it.

Create a file .pythonrc

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

then in your .bashrc file, add

export PYTHONSTARTUP=~/.pythonrc

That seems to work.


回答 1

我认为django的功能类似于https://docs.python.org/library/rlcompleter.html

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

I think django does something like https://docs.python.org/library/rlcompleter.html

If you want to have a really good interactive interpreter have a look at IPython.


回答 2

为了便于记录,本教程将对此进行介绍:http : //docs.python.org/tutorial/interactive.html

For the record, this is covered in the tutorial: http://docs.python.org/tutorial/interactive.html


回答 3

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

ptpython是一个很棒的工具自动完成外壳cmd。安装ptpython非常简单,使用pip工具

pip安装ptpython

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

导入操作系统

os.environ.setdefault(“ DJANGO_SETTINGS_MODULE”,“ testweb.settings”)

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

I use ptpython. https://github.com/jonathanslenders/ptpython/

ptpython is a wonderful tool autocomplete shell cmd. install ptpython is very easy,use pip tool

pip install ptpython

and for django shell,you should import the django env,like this

import os

os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “testweb.settings”)

Trust me,this is the best way to you!!!


回答 4

修复Windows10 Shell:

  • 点安装pyreadline
  • 点安装ipython [shell]

fix for windows10 shell:

  • pip install pyreadline
  • pip install ipython[shell]

回答 5

看起来python3开箱即用!

It looks like python3 has it out-of box!


回答 6

在Python3中,默认情况下启用此功能。我的系统未readline安装模块。我在Manjaro。我没有在其他Linux发行版(基本版,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中的制表符完成,而没有对.pythonrcand的任何创建/更改.bashrc

In Python3 this feature is enabled by default. My system didn’t have the module readline installed. I am on Manjaro. I didn’t face this tab completion issue on other linux distributions (elementary, ubuntu, mint).

After pip installing the module, while importing, it was throwing the following error-

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

To solve this, I ran-

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

This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.


回答 7

是。它内置于3.6。

fernanr @ gnuruwi〜$ python3.6 Python 3.6.3(默认,Apr 10 2019,14:37:36)[Linux上的GCC 4.8.5 20150623(Red Hat 4.8.5-16)]键入“ help”,“ copyright ”,“信用”或“许可证”以获取更多信息。

导入操作系统。显示所有318种可能性?(y或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.getppid( .st

Yes. It’s built in to 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


回答 8

对于旧版本(2.x),上述脚本的工作方式类似于charm :)

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                 

For older versions (2.x) above script works like charm :)

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