在IPython中运行Django shell

Alg*_*bra 27 django

我试图在IPython中运行Django shell:

ipython manage.py shell
Run Code Online (Sandbox Code Playgroud)

它报告以下错误:

键入"manage.py help"以获取有关特定子命令的帮助.

我搜索并找到了一个流行的解决方案,可以指导您安装django.extensions并启用它settings.

我正在创建一个最小的项目,而解决方案很重.同时,我必须在每个项目上安装并启用它.

有轻量级的方式吗?

我开始一个新项目, python manage.py shell -i ipython无法工作

$ python manage.py shell -i ipython
CommandError: Couldn't import ipython interface.
$ ipython --version
6.4.0
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 52

只需安装ipython到您的virtualenv.manage.py shell应该默认使用它.

您还可以使用manage.py shell-i选项明确选择一个shell(这些选项ipython,bpythonpython用于常规的Python壳):

python manage.py shell -i ipython
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,您都需要安装ipython到您的环境中,但您无需对项目设置进行任何更改.

  • 我更喜欢这种方式。它引导我直接使用浏览器而不是 ipython shell。/sf/answers/3839925631/ (2认同)

hyg*_*ull 14

我很欣赏上面的答案,并基于此我写了这个答案。

\n

如果ipython已经安装在您的系统中virtualenv,那么下面的任何命令都将工作相同(在带有示例片段的命令之后已经在下面介绍了)。

\n
    \n
  • python manage.py shell
  • \n
  • python manage.py -i ipython
  • \n
\n
\n

How I solved (steps with problem & solution)?

\n
\n

注意:确保您已经激活了您的virtualenv.

\n
\n
    \n
  1. 我没有ipython安装在我的 Django 项目中。以下是我尝试在环境中运行 Django shell 时遇到的错误ipython
  2. \n
\n
(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell -i ipython\nCommandError: Couldn\'t import ipython interface.\n(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$\n
Run Code Online (Sandbox Code Playgroud)\n

接下来我ipython使用以下命令安装。

\n
    \n
  1. pip install ipython
  2. \n
\n
(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ pip install ipython\nCollecting ipython\n  Downloading https://files.pythonhosted.org/packages/05/d7/77b7a1988c99227f52402f93fb0f7e88c97239960516f53907ebbc44149c/ipython-7.11.0-py3-none-any.whl (777kB)\n   ...\n   ...\n     |\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 102kB 524kB/s \nInstalling collected packages: ipython-genutils, six, decorator, traitlets, pygments, ptyprocess, pexpect, wcwidth, prompt-toolkit, backcall, parso, jedi, appnope, pickleshare, ipython\nSuccessfully installed appnope-0.1.0 backcall-0.1.0 decorator-4.4.1 ipython-7.11.0 ipython-genutils-0.2.0 jedi-0.15.2 parso-0.5.2 pexpect-4.7.0 pickleshare-0.7.5 prompt-toolkit-3.0.2 ptyprocess-0.6.0 pygments-2.5.2 six-1.13.0 traitlets-4.3.3 wcwidth-0.1.7\n(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$\n
Run Code Online (Sandbox Code Playgroud)\n

3.a)python manage.py shell -i ipython

\n
(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell -i ipython\nPython 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) \nType \'copyright\', \'credits\' or \'license\' for more information\nIPython 7.11.0 -- An enhanced Interactive Python. Type \'?\' for help.\n\nIn [1]: exit()\n
Run Code Online (Sandbox Code Playgroud)\n

3.b)python manage.py shell

\n
(venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell\nPython 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) \nType \'copyright\', \'credits\' or \'license\' for more information\nIPython 7.11.0 -- An enhanced Interactive Python. Type \'?\' for help.\n\nIn [1]:  \n
Run Code Online (Sandbox Code Playgroud)\n

谢谢。

\n