How to save all variables in current python session?

I want to save all variables in my current python environment. One option seems to be to use the pickle module. However, I do not want to do this for two reasons:

1) I need to call pickle.dump () for each variable
2) When I want to get the variables, I have to remember the order in which I saved the variables, and then do pickle.load () to retrieve each variable.

I am looking for some command that will save the whole session, so when I load this saved session, all my variables will be restored. Is it possible?

Thank you very much!
Gaurav

Edit: I think I'm not against calling pickle.dump () for every variable that I would like to save, but remembering the exact order in which the variables were saved seems like a big limitation. I want to avoid this.

+46
python save
Jun 02 '10 at 19:17
source share
7 answers

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

Postpone work:

 import shelve T='Hiya' val=[1,2,3] filename='/tmp/shelve.out' my_shelf = shelve.open(filename,'n') # 'n' for new for 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() 

Recovery:

 my_shelf = shelve.open(filename) for key in my_shelf: globals()[key]=my_shelf[key] my_shelf.close() print(T) # Hiya print(val) # [1, 2, 3] 
+52
Jun 02 '10 at 19:46
source share

Having stayed here and could not save globals() as a dictionary, I found that you can sort the session using the dill library.

This can be done using:

 import dill #pip install dill --user filename = 'globalsave.pkl' dill.dump_session(filename) # and to load the session again: dill.load_session(filename) 
+20
Feb 09 '16 at 15:37
source share

What you are trying to do is sleep mode. This has already been said. The conclusion is that when you try to do this, there are several problems that are difficult to solve. For example, when restoring open file descriptors.

It is better to think about a serialization / deserialization subsystem for your program. In many cases this is not trivial, but a much better solution in the long run.

Although I exaggerated the problem. You can try to define your global dict variables. Use globals() to access the dictionary. Since it is indexed by name, you do not have to worry about ordering.

+2
Jun 02 '10 at 19:26
source share

The following is a way to save Spyder workspace variables using spyderlib functions

 #%% Load data from .spydata file from spyderlib.utils.iofuncs import load_dictionary globals().update(load_dictionary(fpath)[0]) data = load_dictionary(fpath) #%% Save data to .spydata file from spyderlib.utils.iofuncs import save_dictionary def variablesfilter(d): from spyderlib.widgets.dicteditorutils import globalsfilter from spyderlib.plugins.variableexplorer import VariableExplorer from spyderlib.baseconfig import get_conf_path, get_supported_types data = globals() settings = VariableExplorer.get_settings() get_supported_types() data = globalsfilter(data, check_all=True, filters=tuple(get_supported_types()['picklable']), exclude_private=settings['exclude_private'], exclude_uppercase=settings['exclude_uppercase'], exclude_capitalized=settings['exclude_capitalized'], exclude_unsupported=settings['exclude_unsupported'], excluded_names=settings['excluded_names']+['settings','In']) return data def saveglobals(filename): data = globalsfiltered() save_dictionary(data,filename) #%% savepath = 'test.spydata' saveglobals(savepath) 

Let me know if this works for you. David BN

+2
Mar 17 '15 at 10:40
source share

One very easy way that can satisfy your needs. It was very good for me:

Just click this icon in the Variable Explorer (to the right of the Spider):

Saving all variables in * .spydata format

Download all variables or pictures, etc.

+1
Feb 18 '17 at 6:13
source share

If you want the accepted answer to be assigned to a function, you can use:

  import shelve def save_workspace(filename, names_of_spaces_to_save, dict_of_values_to_save): ''' filename = location to save workspace. names_of_spaces_to_save = use dir() from parent to save all variables in previous scope. -dir() = return the list of names in the current local scope dict_of_values_to_save = use globals() or locals() to save all variables. -globals() = Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called). -locals() = Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks. Example of globals and dir(): >>> x = 3 #note variable value and name bellow >>> globals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'x': 3, '__doc__': None, '__package__': None} >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'x'] ''' print 'save_workspace' print 'C_hat_bests' in names_of_spaces_to_save print dict_of_values_to_save my_shelf = shelve.open(filename,'n') # 'n' for new for key in names_of_spaces_to_save: try: my_shelf[key] = dict_of_values_to_save[key] except TypeError: # # __builtins__, my_shelf, and imported modules can not be shelved. # #print('ERROR shelving: {0}'.format(key)) pass my_shelf.close() def load_workspace(filename, parent_globals): ''' filename = location to load workspace. parent_globals use globals() to load the workspace saved in filename to current scope. ''' my_shelf = shelve.open(filename) for key in my_shelf: parent_globals[key]=my_shelf[key] my_shelf.close() an example script of using this: import my_pkg as mp x = 3 mp.save_workspace('a', dir(), globals()) 

To get / download a workspace:

 import my_pkg as mp x=1 mp.load_workspace('a', globals()) print x #print 3 for me 

It worked when I ran it. I admit that I do not understand dir() and globals() 100%, so I'm not sure if there might be some strange warning, but for now it works. Comments are welcome :)




after some additional research, if you call save_workspace , as I suggested using global variables, and save_workspace inside the function, it will not work properly if you want to save the verification in the local area. To do this, use locals() . This is due to the fact that global variables take global values ​​from the module where the function is defined, and not from where it is called, it will be my guess.

0
Jul 13 '16 at 20:23
source share

I wanted to add a comment to the unutbu answer, but I don't have enough reputation yet. I also had a problem with

 PicklingError: Can't pickle <built-in function raw_input>: it not the same object as __builtin__.raw_input 

I went around it, adding a rule to pass all the exceptions and tell me that it did not store. unubtu tweaked code, for convenience copypaste:

Postpone work:

 import shelve filename='/tmp/shelve.out' my_shelf = shelve.open(filename,'n') # 'n' for new for key in dir(): try: my_shelf[key] = globals()[key] except TypeError: # # __builtins__, my_shelf, and imported modules can not be shelved. # print('TypeError shelving: {0}'.format(key)) except: # catches everything else print('Generic error shelving: {0}'.format(key)) my_shelf.close() 

Recovery:

 my_shelf = shelve.open(filename) for key in my_shelf: globals()[key]=my_shelf[key] my_shelf.close() 
0
Nov 21 '16 at 17:45
source share



All Articles