128

I want to save all the variables in my current python environment. It seems one option is to use the 'pickle' module. However, I don't want to do this for 2 reasons:

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

I am looking for some command which would save the entire session, so that when I load this saved session, all my variables are restored. Is this possible?

Edit: I guess I don't mind calling pickle.dump() for each variable that I would like to save, but remembering the exact order in which the variables were saved seems like a big restriction. I want to avoid that.

7 Answers 7

98

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 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()

To restore:

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]
11
  • 7
    Perfect. This is what I was looking for. BTW, I find this sentence in your post super funny: "To shelve your work" :)
    – user10
    Jun 2, 2010 at 19:58
  • 4
    And here I thought "pickles" were funny! :) en.wikipedia.org/wiki/Inherently_funny_word
    – unutbu
    Jun 2, 2010 at 20:12
  • 2
    I know this answer is very old byt when I do this I have the following error: PicklingError: Can't pickle <built-in function raw_input>: it's not the same object as __builtin__.raw_input I just have 2 variables declared in my workspace. Any ideas on how to solve this? Has some better way to save the current session come out after this answer?
    – hellter
    Sep 4, 2016 at 9:32
  • 1
    I have the same problem regarding the usage of shelve as described above. PicklingError: Can't pickle <type 'numpy.int32'>: it's not the same object as numpy.int32
    – Pu Zhang
    Oct 17, 2016 at 13:33
  • 2
    It looks like some builtin functions and packages won't be able to be shelved so just use except: instead of except TypeError:. This will shelve user defined variables and most objects (pandas data frames shelved fine for me)
    – Nitro
    Feb 7, 2017 at 21:51
90

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 --user
filename = 'globalsave.pkl'
dill.dump_session(filename)

# and to load the session again:
dill.load_session(filename)
7
  • I don't think dill saves all the variables, for example if you run dill.dump_session() in a function variables which are local to that function aren't saved.
    – Parsa
    Mar 5, 2018 at 19:39
  • 5
    Thats just a scope issue, I guess you could just append all your locals() to globals() if you must? Mar 6, 2018 at 11:52
  • I got "TypeError: can't pickle Socket objects"
    – R. Cox
    Sep 27, 2019 at 10:42
  • 1
    I get the following type error when dumping the session: TypeError: no default __reduce__ due to non-trivial __cinit__
    – KareemJ
    Oct 29, 2019 at 8:36
  • 1
    I tried this and found it can't save named arrays though this might be a pickle limitation.
    – rhody
    Nov 21, 2019 at 17:48
13

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.

3
  • 1
    I saved all variables in .spydata format yesterday and I tried to import data today. No variable gets imported :( Jun 28, 2019 at 9:03
  • This was working for me but now that I have more data it, rather than making a Spydata file, now makes a pickle file with zero contents as well as hundreds of npy files. How do I open these please?
    – R. Cox
    Sep 27, 2019 at 10:47
  • commenting in 2023, I can no longer import spyder data files into spyder for whatever reason
    – d3hero23
    Feb 27, 2023 at 18:07
4

Here is a way saving the Spyder workspace variables using the 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 it works for you. David B-H

3
  • "NameError: name 'fpath' is not defined": did I forget something?
    – Thomas
    Aug 26, 2015 at 11:08
  • That's a good idea. I was thinking about borrowing from spyder's workspace for the same thing. But didn't figure out how. However, I didn't quite understand your code. Could you please tell, does this work exactly like Spyder that it automatically catches all the varibles, or I have to specify the variables I want to use?
    – ZK Zhao
    Jun 6, 2016 at 10:28
  • @DavidBH the spyderlib does not exist yet I can run spyder
    – d3hero23
    Feb 27, 2023 at 18:11
2

What you're trying to do is to hibernate your process. This was discussed already. The conclusion is that there are several hard-to-solve problems exist while trying to do so. For example with restoring open file descriptors.

It is better to think about serialization/deserialization subsystem for your program. It is not trivial in many cases, but is far better solution in long-time perspective.

Although if I've exaggerated the problem. You can try to pickle your global variables dict. Use globals() to access the dictionary. Since it is varname-indexed you haven't to bother about the order.

2
  • Nopes. I am not trying to hibernate the process. I have an interactive python shell on which I run several scripts and commands. I want to save the output (variables) of some of these commands, so that in future whenever I need access to the output, I can just fire up a python shell and load all these variables.
    – user10
    Jun 2, 2010 at 19:29
  • So, pickle the dictionary var_name -> var_value
    – nkrkv
    Jun 2, 2010 at 19:33
2

If you want the accepted answer abstracted to 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/load the 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 will admit I don't understand dir() and globals() 100% so I am not sure if there might be some weird caveat, but so far it seems to work. Comments are welcome :)


after some more research if you call save_workspace as I suggested with globals and save_workspace is within a function it won't work as expected if you want to save the veriables in a local scope. For that use locals(). This happens because globals takes the globals from the module where the function is defined, not from where it is called would be my guess.

0

You can save it as a text file or a CVS file. People use Spyder for example to save variables but it has a known issue: for specific data types it fails to import down in the road.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.