Run Django shell in IPython
DjangoDjango Problem Overview
I attempt to run a Django shell in IPython:
ipython manage.py shell
It reports the following error:
> Type 'manage.py help
I searched and found a popular solution which guides you to install django.extensions
and to enable it in settings
.
I am creating a minimal project while the solution is heavy. Meanwhile, I have to install and enable it on every project.
Is there a lightweight way?
I start a new project,
python manage.py shell -i ipython
fails to work
$ python manage.py shell -i ipython
CommandError: Couldn't import ipython interface.
$ ipython --version
6.4.0
Django Solutions
Solution 1 - Django
Simply install ipython
into your virtualenv. manage.py shell
should use it by default.
You can also use manage.py shell
with the -i
option to explicitly select a shell (the options are ipython
, bpython
, or python
for a regular Python shell):
python manage.py shell -i ipython
Either way, you do need to install ipython
into your environment, but you don't need to make any changes to your project settings.
Solution 2 - Django
I appreciate above answer and based on that I have written this answer.
If ipython
is already installed in your virtualenv
then any of the below command will work same (which has been already presented below after commands with sample snippets, point wise).
python manage.py shell
python manage.py -i ipython
How I solved (steps with problem & solution)?
> Note: Make sure you have already activated your virtualenv
.
- I didn't have
ipython
installed in my Django project. Below is the error I got while trying to run Django shell inipython
environment.
(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$
Next I installed ipython
using below command.
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]:
Thank you.