【问题标题】:Run Django shell in IPython在 IPython 中运行 Django shell
【发布时间】:2018-04-20 13:40:58
【问题描述】:

我尝试在 IPython 中运行 Django shell:

ipython manage.py shell

报如下错误:

键入“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

【问题讨论】:

    标签: django


    【解决方案1】:

    只需将ipython 安装到您的虚拟环境中。 manage.py shell 应该默认使用它。

    您还可以使用manage.py shell-i option 来显式选择一个shell(对于常规的Python shell,选项是ipythonbpythonpython):

    python manage.py shell -i ipython
    

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

    【讨论】:

    【解决方案2】:

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

    如果 ipython 已经安装在您的 virtualenv 中,那么下面的任何命令都可以正常工作(在带有示例 sn 的命令之后已经显示-ps,逐点)。

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

    How I solved (steps with problem & solution)?

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

    1) 我的 Django 项目中没有安装 ipython。以下是我在 ipython 环境中尝试运行 Django shell 时遇到的错误。

    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell -i ipython
    CommandError: Couldn't import ipython interface.
    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$
    

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

    2)pip install ipython

    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ pip install ipython
    Collecting ipython
      Downloading https://files.pythonhosted.org/packages/05/d7/77b7a1988c99227f52402f93fb0f7e88c97239960516f53907ebbc44149c/ipython-7.11.0-py3-none-any.whl (777kB)
       ...
       ...
         |████████████████████████████████| 102kB 524kB/s 
    Installing collected packages: ipython-genutils, six, decorator, traitlets, pygments, ptyprocess, pexpect, wcwidth, prompt-toolkit, backcall, parso, jedi, appnope, pickleshare, ipython
    Successfully 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
    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$
    

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

    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell -i ipython
    Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.11.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: exit()
    

    3.b) python manage.py shell

    (venv3.6.7) Rishikeshs-MacBook-Air:src hygull$ python manage.py shell
    Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.11.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]:  
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2011-01-07
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2015-01-12
      相关资源
      最近更新 更多