16

I´m trying to restart the kernel in a Google Colab Jupyter Notebook through a cell. The option given previously:

import os
os._exit(00)

is ok, but it seems to me that this is not a very "pythonic" way of restarting the kernel. The other option:

from IPython.core.display import HTML
HTML("<script>Jupyter.notebook.kernel.restart()</script>")

seems more "pythonic" (better) to me, but it is not working.

Is there something specific to Google Colab that I should have done?

Best regards,

Gustavo,

Gustavo Mirapalheta
  • 931
  • 2
  • 11
  • 25
  • 1
    `os.exit` works. Is there some issue with that approach that is causing you to pursue an alternative? – Bob Smith Mar 05 '19 at 19:32
  • Thanks for your answer Bob. Nothing special. It is just that crashing the kernel seemed to me a "rash" alternative. The other option (using a command through the Jupyter notebook interface) seemed more elegant. In any case, your answer was excelent. Worked like a charm!. Best regards, – Gustavo Mirapalheta Mar 05 '19 at 19:40

3 Answers3

8

Just go to "Runtime" in the option menu then click on "Reset all runtimes..".

It helps me to restart kernel as in jupyter notebook happen when i'm trying to run this:

tf.executing_eagerly()
Qbert
  • 326
  • 3
  • 5
1

I used to use this command:

condacolab KERNEL RESTART \
print("Restarting of kernel...") \
get_ipython().kernel.do_shutdown(True)
Viktor Ivliiev
  • 1,015
  • 4
  • 14
  • 21
0

Write this on a cell and execute:

import os
os.kill(os.getpid(), 9)
Sam Masri
  • 51
  • 4