Python auto-reloading REPL

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
%autoreload 2

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:

The autoreload mechanisms runs after you try to run something in IPython. Therefore you shouldn't worry about incurring in the overhead of constantly reloading modules.

Fast state loading

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 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 = [
    '%autoreload 2',
    'import something as foo'
c.InteractiveShellApp.exec_files = [

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

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:

IPython with Django

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

$ python 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 in the same folder than

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