loadobj#
- loadobj(filename=None, folder=None, verbose=None, die=False, remapping=None, method=None, auto_remap=True, **kwargs)#
Load a file that has been saved as a gzipped pickle file, e.g. by
sc.save()
. Accepts either a filename (standard usage) or a file object as the first argument. Note thatsc.load()
/sc.loadobj()
are aliases of each other.Note 1: Since this function relies on pickle, it can potentially execute arbitrary code, so you should only use it with sources you trust. For more information, see: https://docs.python.org/3/library/pickle.html
Note 2: When a pickle file is loaded, Python imports any modules that are referenced in it. This is a problem if module has been renamed. In this case, you can use the
remapping
argument to point to the new modules or classes. For more robustness, use thesc.savearchive()
/sc.loadarchive()
functions.- Parameters:
filename (str/Path) – the filename (or full path) to load
folder (str/Path) – the folder (not needed if the filename includes it)
verbose (bool) – print nothing (False), critical warnings (None), or full detail (True)
die (bool) – whether to raise an exception if errors are encountered (otherwise, load as much as possible via the ‘robust’ method)
remapping (dict) – way of mapping old/unavailable module names to new (see below for example)
method (str) – method for loading (‘pickle’, ‘dill’, ‘pandas’, or ‘robust’; if None, try all)
auto_remap (bool) – whether to use known deprecations to load failed pickles
kwargs (dict) – passed to
pickle.loads()
/dill.loads()
Examples:
obj = sc.load('myfile.obj') # Standard usage old = sc.load('my-old-file.obj', method='dill', ignore=True) # Load classes from saved files old = sc.load('my-old-file.obj', remapping={'foo.Bar': cat.Mat}) # If loading a saved object containing a reference to foo.Bar that is now cat.Mat old = sc.load('my-old-file.obj', remapping={('foo', 'Bar'): ('cat', 'Mat')}, method='robust') # Equivalent to the above but force remapping and don't fail old = sc.load('my-old-file.obj', remapping={'foo.Bar': None}) # Skip mapping foo.Bar and don't fail
New in version 1.1.0: “remapping” argumentNew in version 1.2.2: ability to load non-gzipped pickles; support for dill; arguments passed to loaderNew in version 3.1.0: improved handling of pickling failuresNew in version 3.1.1: allow remapping toNone