Saving/Restoring a Jupyter Notebook Session

DS4N6: Tips and Tricks
Oct 27, 2020

Originally published at

Say you have been working a lot in your current notebook, you have lots of DataFrames and Series that get a long time to be computed, and you want to make sure you recover quickly if, for some reason, your Session (Kernel) dies.

The answer is “dill”.


pip install dill

Save a Notebook session:

import dill dill.dump_session('notebook_env.db')

Restore a Notebook session:

import dill dill.load_session('notebook_env.db')


  • Fails when there are generators.
  • If you have pyodbc connection objects hanging around, you’ll need to close them and then set them all to None otherwise, you get a “TypeError: can’t pickle pyodbc.Connection objects” error.
  • The notebook state does not include graphs that were generated by your code, so you’ll need to rerun the cells to bring these back.




DS4N6: Tips and Tricks

Community focused on bringing Data Science & Artificial Intelligence to the fingertips of the average Forensicator and promoting advances in the field.