There is a way to reset the Jupyter kernel's cache besides restarting the kernel?

In Jupyter Notebook, the kernel can be manually reset to apply new changes to modules and packages. With Python, code can be written to restart the kernel, but all code execution is terminated once the kernel is reset. The thought in question now is whether there is a cache for a Jupyter notebook or kernel that can be reset with Python code to allow updates to be used in code execution?

Thanks for your help on this question.

For IPython/ipykernel, you can try the various reload mechanisms:

https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html

But otherwise: the dynamism of python, and the sheer number of files involved, makes it hard to anticipate when changes should change code… and what would it even mean, once you get multiple inheritance, etc. You can use this dynamism yourself with monkeypatching, etc. but coordinating hot instances with on-disk changes is still going to be challenging.

A useful approach to try when working locally: if there’s “expensive” artifacts generated, write them off to disk, and have code paths that can work with those files. Then, at least, Restart and Run All will still be accurate, above all, and at least be faster than recalculating everything from scratch.

2 Likes

Does this autoreload magic command work for pip installed Python packages that are installed via their setup.py files? I’ve tried running this command in a Python script and in JupyterLab, but a module updated in the Python package after reinstallation has not been reloaded successfully.