Restart ipython Kernel with a command from a cell

2020-05-30 08:53发布

问题:

Is it possible to restart an ipython Kernel NOT by selecting Kernel > Restart from the notebook GUI, but from executing a command in a notebook cell?

回答1:

As Thomas K. suggested, here is the way to restart the ipython kernel from your keyboard:

import os
os._exit(00)


回答2:

To define a function that restarts the Jupyter kernel, I've successfully used:

from IPython.display import display_html
def restartkernel() :
    display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)

then calling

restartkernel()

when time for the restart.