This tutorial describes you can to reload your source code with
IPython alone or together with
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
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
0: No autoreloading.
1: Autoreloading only for modules imported with
2: Autoreload everything that was not excluded with
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.
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 = [ 'autoreload' ] c.InteractiveShellApp.exec_lines = [ '%autoreload 2', '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.extensionsis used to load the extensions that
exec_lineswill need. You need to load here all the extensions you need in the statup process.
InteractiveShellApp.exec_linesholds the strings that will be interprepred in the startup.
InteractiveShellApp.exec_filesinstead of executing lines of code you can also load complete files.
In the example there is an
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 change the default interpreter of the django shell with the
--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
In this situation we would have two terminals open, one for the our python shell and the other for the normal