Autoreload in Ipython


πŸŽ„This post has fully grown

Autoreload in Ipython

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/

c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in 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 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 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.

πŸ‘€ see an issue, edit this post on GitHub

If you found value in this post
and want to send a tip.

Buy Me A Coffee

tweet about this post and it will show up here.

←An IndieWeb Webring πŸ•ΈπŸ’β†’