How can I save and load state of the kernel?

73 views
Skip to first unread message

Wil C

unread,
Aug 3, 2017, 2:41:25 PM8/3/17
to Project Jupyter
Hi all,

In the course of a normal use of a notebook, the output of each cell is saved when the notebook is saved. However, if I closed the notebook and opened it again, the variables in the namespace all go away (presumably because the kernel was restarted). Is there a way to save the variables in a kernel namespace and load it again, so I don't have to go through and run every cell?

Wil

Thomas Kluyver

unread,
Aug 3, 2017, 3:03:35 PM8/3/17
to Project Jupyter
Hi Wil,

There's nothing quite like that built into IPython or Jupyter, although the %store magic in IPython can be used to save individual variables.

The dill package (https://pypi.python.org/pypi/dill ) has functions to save and restore all the variables in a session. But be aware that there are some things (like open file handles or sockets) which can't really be saved.

If you're feeling adventurous, there are tools that do checkpoint-restart, which work at a lower level than Python to snapshot the whole of a process so you can resume it later.

Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/a1b16305-9ba7-4f91-afae-640ec45c85cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wil C

unread,
Aug 3, 2017, 7:48:19 PM8/3/17
to Project Jupyter
I looked into this a little afterwards, and I was thinking about it solely from an iPython perspective. Shouldn't I be able to just traverse all the variables in the Python space using reflection, and store them all with Pickle and Shelf? And then restore it when a new notebook is started?

Wil


On Thursday, August 3, 2017 at 12:03:35 PM UTC-7, takowl wrote:
Hi Wil,

There's nothing quite like that built into IPython or Jupyter, although the %store magic in IPython can be used to save individual variables.

The dill package (https://pypi.python.org/pypi/dill ) has functions to save and restore all the variables in a session. But be aware that there are some things (like open file handles or sockets) which can't really be saved.

If you're feeling adventurous, there are tools that do checkpoint-restart, which work at a lower level than Python to snapshot the whole of a process so you can resume it later.

Thomas
On 3 August 2017 at 19:41, Wil C <iam...@gmail.com> wrote:
Hi all,

In the course of a normal use of a notebook, the output of each cell is saved when the notebook is saved. However, if I closed the notebook and opened it again, the variables in the namespace all go away (presumably because the kernel was restarted). Is there a way to save the variables in a kernel namespace and load it again, so I don't have to go through and run every cell?

Wil

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

Thomas Kluyver

unread,
Aug 4, 2017, 3:46:46 AM8/4/17
to Project Jupyter
On 4 August 2017 at 00:48, Wil C <iam...@gmail.com> wrote:
I looked into this a little afterwards, and I was thinking about it solely from an iPython perspective. Shouldn't I be able to just traverse all the variables in the Python space using reflection, and store them all with Pickle and Shelf? And then restore it when a new notebook is started?

Yep. This is essentially what dill is doing. Dill also extends pickle to cover some things that pickle can't normally handle.

Wil Chung

unread,
Aug 4, 2017, 5:51:19 PM8/4/17
to jup...@googlegroups.com
Ah! Dill is so much better, and exactly what I was looking for. It's able to save and load the entire python interpreter session. Thanks!

Wil

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/k3oC4ANRxW0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Denis Akhiyarov

unread,
Aug 4, 2017, 11:56:09 PM8/4/17
to Project Jupyter


On Friday, August 4, 2017 at 4:51:19 PM UTC-5, Wil C wrote:
Ah! Dill is so much better, and exactly what I was looking for. It's able to save and load the entire python interpreter session. Thanks!

Wil
On Fri, Aug 4, 2017 at 12:46 AM, Thomas Kluyver <tak...@gmail.com> wrote:
On 4 August 2017 at 00:48, Wil C <iam...@gmail.com> wrote:
I looked into this a little afterwards, and I was thinking about it solely from an iPython perspective. Shouldn't I be able to just traverse all the variables in the Python space using reflection, and store them all with Pickle and Shelf? And then restore it when a new notebook is started?

Yep. This is essentially what dill is doing. Dill also extends pickle to cover some things that pickle can't normally handle.

--
You received this message because you are subscribed to a topic in the Google Groups "Project Jupyter" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jupyter/k3oC4ANRxW0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jupyter+u...@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages