How to save all the variables in the current python session? How to save all the variables in the current python session? python python

How to save all the variables in the current python session?


If you use shelve, you do not have to remember the order in which the objects are pickled, since shelve gives you a dictionary-like object:

To shelve your work:

import shelveT='Hiya'val=[1,2,3]filename='/tmp/shelve.out'my_shelf = shelve.open(filename,'n') # 'n' for newfor key in dir():    try:        my_shelf[key] = globals()[key]    except TypeError:        #        # __builtins__, my_shelf, and imported modules can not be shelved.        #        print('ERROR shelving: {0}'.format(key))my_shelf.close()

To restore:

my_shelf = shelve.open(filename)for key in my_shelf:    globals()[key]=my_shelf[key]my_shelf.close()print(T)# Hiyaprint(val)# [1, 2, 3]


Having sat here and failed to save the globals() as a dictionary, I discovered you can pickle a session using the dill library.

This can be done by using:

import dill                            #pip install dill --userfilename = 'globalsave.pkl'dill.dump_session(filename)# and to load the session again:dill.load_session(filename)


One very easy way that might satisfy your needs. For me, it did pretty well:

Simply, click on this icon on the Variable Explorer (right side of Spider):

Saving all the variables in *.spydata format

Loading all the variables or pics etc.