Skip to content

Instantly share code, notes, and snippets.

@k-bx
Created July 6, 2013 17:18
Show Gist options
  • Save k-bx/5940568 to your computer and use it in GitHub Desktop.
Save k-bx/5940568 to your computer and use it in GitHub Desktop.
How to launch ipython manage.py shell in "classic" python prompt

How to launch ipython manage.py shell in "classic" python prompt

I wrote a python django library and wanted to capture some classic "python prompt documentation" for it. It turned out to be harder then I thought.

First of all, you are no longer able to run regular python (maybe I missed something) via

python manage.py shell

Django still happily runs ipython for you.

Second, once you've installed ipython, when you run

ipython manage.py shell

django will happily "embed()" new session of ipython for you, so no matter which parameters you provide to your "ipython", they won't work. So, this --classic (which turns out good old >>> prompt and all other python-mimic) won't work here:

ipython manage.py shell --classic

Solution

Quick and dirty fix without going to all sort of upstreams asking for better API is to go to django inside your virtualenv and add explicit config-passing as shown below in python-file.

# /home/you/.virtualenvs/your-env/local/lib/python2.7/site-packages/django/core/management/commands/shell.py
# ...
def ipython(self):
try:
from IPython import embed
from IPython.config.loader import Config
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
classic_config.PromptManager.in_template = '>>> '
classic_config.PromptManager.in2_template = '... '
classic_config.PromptManager.out_template = ''
classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
classic_config.InteractiveShell.colors = 'NoColor'
classic_config.InteractiveShell.xmode = 'Plain'
embed(config=classic_config)
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment