3

I want to write a function 'backup(filename)' to store all the working data (objects?) under current environment of Python, and 'restore(filename)' to restore the data/object again. Just like R's save.image(file="workspace21.RData") and load(file="workspace21.RData"), which can snapshot the system.

How to write the "backup" & "restore" ? Or is there any package existed can do that ?

bigbug
  • 55,954
  • 42
  • 77
  • 96
  • You mean like [pickle](http://docs.python.org/2/library/pickle.html)? – dwxw Jul 11 '13 at 08:41
  • somewhat like pickle. I need it to pickle the whole workspace , but some of python objects are not_picklable, so I doubt pickle is the solution. – bigbug Jul 11 '13 at 08:47
  • For example, matplotlib.figure.Figure object is not pickleable, cPickle.dump(fig, open("Z:\\pic.pkl",'w'), cPickle.HIGHEST_PROTOCOL) will have TypeError: expected string or Unicode object, NoneType found – bigbug Jul 11 '13 at 08:59
  • Hmm, I found [this](http://stackoverflow.com/questions/10390521/caching-matplotlib-with-memcache-wont-pickle) which sounds related to trying to pickle matplotlib figures. @pelson says version 1.2 should support pickling, but without trying it myself I can't comment. – dwxw Jul 11 '13 at 09:16
  • matplotlib figure is only a sample, there might exists other kinds of not-picklable objects . – bigbug Jul 11 '13 at 09:24

3 Answers3

3

pickle module seems like a solution but it cannot really save the whole environment for you. Try for example this:

import pickle

def backup(fileName):
    pickle.dump(globals(), open(fileName,'w'), pickle.HIGHEST_PROTOCOL)

def restore(fileName):
    globals().update(pickle.load(open(fileName,"rb"))) 

This will not work because module objects are not picklable. You will also have problems with open file descriptors and other objects. See answers on this question for (partial) solutions:

How can I save all the variables in the current python session?

So, while you cannot have a general solution to your problem, you can write something which will save some of your (global) objects, using the snippet above as a starting point, if that helps.

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • One more issue for me is how to load the stored variable from pickled file to global() namespace by a function. def restore(fileName): cPickle.load(open(fileName,"rb")) seems not able to restore the variable back to global namespace . – bigbug Jul 11 '13 at 09:20
  • 1
    You could do something like: globals().update(cPickle.load(open(fileName,"rb"))) – piokuc Jul 11 '13 at 11:11
3

A bit out of the box, but if this is important and you really need a full solution, you can run python inside a virtual machine and use snapshots to save session state. Whether it is practical or not depends on your use case.

Paulo Almeida
  • 7,803
  • 28
  • 36
0

You should take a look on pickle documentation. You also have an example that can help you to do what you're thinking on.

José L. Patiño
  • 3,683
  • 2
  • 29
  • 28