135

使用 启动 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 应用程序的情况下启动一个交互式控制台?

4

9 回答 9

223

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

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

这似乎行得通。

于 2008-10-29T13:24:39.607 回答
35

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

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

于 2008-10-29T13:21:43.823 回答
27

作为记录,这在教程中有所介绍:http: //docs.python.org/tutorial/interactive.html

于 2008-10-29T16:39:03.053 回答
13

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

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

pip install ptpython

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

import os

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

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

于 2015-08-21T08:11:38.863 回答
7

修复 Windows 10 外壳:

pip install pyreadline3  # previously, pyreadline but that package was abandoned
pip install ipython
于 2019-03-05T21:42:10.193 回答
3

看起来 python3 开箱即用!

于 2017-03-23T21:50:33.263 回答
1

在 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 中的选项卡完成,而没有任何创建/更改.pythonrcand .bashrc

于 2017-07-02T19:44:48.970 回答
0

是的。它内置于 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
于 2020-02-11T07:09:08.420 回答
-1

对于旧版本(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                 
于 2020-02-11T07:16:50.703 回答