This tutorial describes you can to reload your source code with IPython alone or together with django.

ipython is like the normal python console, but more powerful:

IPython (Interactive Python) is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history. - Wikipedia

## How to use IPython autoreload

We should make use the autoreload IPython extension. What basically means using two magic commands:

%load_ext autoreload


You may think that is all you need to know. Maybe, but there is a little bit more that could be interesting. You can tell ipython to autoreload everything by default, or to only reload specific modules. To understand how we can do that, we need to know the arguments that %autoreload accepts:

• 0: No autoreloading.
• 1: Autoreloading only for modules imported with %aimport
• 2: Autoreload everything that was not excluded with %aimport -foo_module.

Another important aspect of a convenient interactive development cycle is to be able to automatically change the state or execute code at the startup. This is facilitated by the ipython_config.py file. IPython executes it when starting. It is not a normal python file, but a configuration file you use to setup IPython and maybe execute some code.

Lets see a basic configuration example:

c = get_config()
c.InteractiveShellApp.extensions = [
]
c.InteractiveShellApp.exec_lines = [
'import something as foo'
]
c.InteractiveShellApp.exec_files = [
'mycode.py',
'othercode.py'
]


What we saw are basically three variables that hold an array of strings:

• InteractiveShellApp.extensions is used to load the extensions that exec_lines will need. You need to load here all the extensions you need in the statup process.
• InteractiveShellApp.exec_lines holds the strings that will be interprepred in the startup.
• InteractiveShellApp.exec_files instead of executing lines of code you can also load complete files.

In the example there is an import inside InteractiveShellApp.exec_lines to show you that you can bring some package into context automatically.

You may think that this configuration isn't really necessary because you can keep the interpreter open indefinitely, but that is not a real life occurrence. In reality, you will need to restart the interpreter many times, in situations like:

• You can reload modules, but what about all the other objects or global state that your old code could generate?
• It is not possible to reload C extension modules.
• Most of the time, decorators can't be reloaded.
• Some other edge case I can't think of.

## IPython with Django

You can change the default interpreter of the django shell with the -i(--interface) option like this:

\$ python manage.py shell -i ipython


Another important aspect is all the auto-reloading stuff, we can do what we saw in the previous section by having the ipythin_config.py in the same folder than manage.py.

In this situation we would have two terminals open, one for the our python shell and the other for the normal runserver command.