[Python] Automagically Reload Imports In iPython!

If you are using iPython, you will be importing some of the modules you have written. While playing with them, you may want to change them or if there is a bug, you will have to fix them. Since you have changed your code, you have to exit and start new instance of the interpreter or you can reload the module.

To reload the already imported modules:

In Python 2.x, you can do directly run
>>> imp.reload(module)
In Python 3.0–3.3 you have to use
>>> imp.reload(module)
In Python 3.4 you have to use
>>> importlib.reload(module)
If you are developing something this will be annoying because for every small change you need to reload the module. iPython has a cool extension called autoreload which automatically reloads modules. You can load it with
In [15]: %load_ext autoreload

In [16]: %autoreload?

%autoreload => Reload modules automatically

%autoreload

Reload all modules (except those excluded by %aimport) automatically now.

%autoreload 0

Disable automatic reloading.

%autoreload 1

Reload all modules imported with %aimport every time before executing

the Python code typed.

%autoreload 2

Reload all modules (except those excluded by %aimport) every time

before executing the Python code typed.
So, if you set autoreload to 2 it reloads modules everytime before executing code.
 In [15]: %autoreload 2

You can do this everytime you start iPython or you can add config iPython to that automatically.
Create a default profile using this command
$ ipython profile create
Go to your default profile at ~/.ipython/profile_default/ipython_config.py  and find these two lines
c.InteractiveShellApp.extensions = []
c.InteractiveShellApp.exec_lines = []
and change them to 
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

Also note that reload doesn't reloads c extensions automatically.