Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running profile startup files in an embedded IPython instance

I want to use an embedded IPython shell with a user_ns dictionary and a my profile configuration (ipython_config.py and the startup files). The purpose is to run a Django shell with models imported on startup. django-extensions implements a command called shell_plus that does this:

https://github.com/django-extensions/django-extensions/blob/master/django_extensions/management/commands/shell_plus.py

from IPython import embed
embed(user_ns=imported_objects)

The problem is that this does not load my startup files. embed() calls load_default_config() which I figure loads ipython_config.py.

How do I make the embedded IPython instance run my profile startup files?

like image 682
hekevintran Avatar asked Apr 08 '12 17:04

hekevintran


1 Answers

I used the following workaround to run my own IPython startup script but still take advantage of shell_plus:

  1. Create a file called shell_plus_startup.py in the same directory as manage.py. For example:

    # File: shell_plus_startup.py
    # Extra python code to run after shell_plus starts an embedded IPython shell.
    # Run this file from IPython using '%run shell_plus_startup.py'
    
    # Common imports
    from datetime import date
    
    # Common variables
    tod = date.today()
    
  2. Launch shell plus (which launches an embedded IPython shell).

    python manage.py shell_plus

  3. Manually run the startup script.

    In [1]: %run shell_plus_startup.py
    
  4. Then you can use variables you've defined, modules you've imported, etc.

    In [2]: tod
    Out[2]: datetime.date(2012, 7, 14)
    

Also see this answer: scripting ipython through django's shell_plus

like image 110
Scott Foubister Avatar answered Nov 15 '22 08:11

Scott Foubister