Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset an IPython kernel without restarting

Is there a way of programmatically reset an IPython kernel without restarting it?

Currently I am calling KernelManager.restart_kernel but that is quite slow.

The purpose of this is to isolate unit tests in a py.test plugin for using Notebook cells as unit tests. If you have other ideas to achieve this I'd love suggestions. The code is here:

https://github.com/zonca/pytest-ipynb/blob/master/pytest_ipynb/plugin.py#L100

like image 479
Andrea Zonca Avatar asked Mar 14 '23 06:03

Andrea Zonca


1 Answers

I used the %reset IPython magic function instead of restarting the kernel. That removes all the variables but does not perform again the imports. That is suitable for my application.

See IPython docs: http://ipython.readthedocs.org/en/stable/interactive/magics.html?highlight=magic#magic-reset

like image 95
Andrea Zonca Avatar answered Mar 24 '23 00:03

Andrea Zonca