I have used %autoreload for several years now with great success and ๐Ÿ”ฅ rapid reloads. It allows me to move super fast when developing libraries and modules. They have made some great updates this year that allows class modules to be automatically be updated.

What I like about autoreload

๐Ÿ”ฅ Blazing Fast

๐Ÿ’ฅ Keeps me in the comfort of my text editor

๐Ÿ‘ Allows me to use Jupyter when I need

๐Ÿ‘Ÿ Extremely Reliable

One of the biggest benefits that I find is that it shortens the distance between my module/library code and test code inside of a terminal/notebook. Now I primarily use jupyter notebooks for the presentation aspect. I develop code from the comfort of my editor with all of the tools I have setup, and run the functions in a notebook to get the output. From there I might do some aggregations or plots, but the ๐Ÿฅฉ meat of development is done outside of jupyter.

Now I primarily use jupyter notebooks for the presentation aspect.

Enabling Autoreload

๐Ÿ“ config

This is a short script that I use to setup ipython so that it automatically reloads modules. This allows me to use a separate terminal and editor, and keep data in memory while developing functions.


ipython profile create

Then edit the created file ~/.ipython/profile_default/ipython_config.py.


c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in ipython_config.py to improve performance.")')

According to the docs

autoreload caveates

Some of the known remaining caveats are:

Replacing code objects does not always succeed: changing a @property in a class to an ordinary method or a method to a member variable can cause problems (but in old objects only). Functions that are removed (eg. via monkey-patching) from a module before it is reloaded are not upgraded. C extension modules cannot be reloaded, and so cannot be autoreloaded.

So what can gets updated??

๐Ÿคฒ Nearly everything...

  • new/updated functions
  • new/updated functions
  • new/updated class methods
  • new/updated class attributes

What does not get updated

๐Ÿ”„ needs restart

config files that are side loaded with modules typically do not get updated in my experience, and I tend to restart the session.

init class methods do not get reran, but the session does not need to be reloaded. The class instance will just need to be re-instanciated.

Testing out the capabilities

๐Ÿ’จ Watch it go

Here is a gif of me taking autoreload out for a test drive. When creating the session test_autoreload.py does not even exist. From there new functions, classes, attributes, and methods are added in the file and all live reload into ipython.

for more gifs like these follow me on twitter @waylonwalker

What About Jupyter Notebooks????

๐Ÿ’ฅ Exactly the Same

Since jupyter uses ipython in be background Jupyter will use the same ipython_config.py file to have autoreload enabled by default.

for more gifs like these follow me on twitter @waylonwalker

Go use it now

Take the splash into rapid development of python functions with minimal distance between your modules/library and your ipython/jupyter session.

Want automatic imports??

pyflyby

This article covers how I setup automatic imports in ipython