loadobj(filename=None, folder=None, verbose=False, die=None, remapping=None, method='pickle', **kwargs)[source]

Load a file that has been saved as a gzipped pickle file, e.g. by sc.saveobj(). Accepts either a filename (standard usage) or a file object as the first argument. Note that loadobj()/load() are aliases of each other.

Note: be careful when loading pickle files, since a malicious pickle can be used to execute arbitrary code.

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.

  • filename (str/Path) – the filename (or full path) to load

  • folder (str/Path) – the folder

  • verbose (bool) – print details

  • die (bool) – whether to raise an exception if errors are encountered (otherwise, load as much as possible)

  • remapping (dict) – way of mapping old/unavailable module names to new

  • method (str) – method for loading (usually pickle or dill)

  • kwargs (dict) – passed to pickle.loads()/dill.loads()


obj = sc.loadobj('myfile.obj') # Standard usage
old = sc.loadobj('my-old-file.obj', method='dill', ignore=True) # Load classes from saved files
old = sc.loadobj('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.loadobj('my-old-file.obj', remapping={'foo.Bar':('cat', 'Mat')}) # Equivalent to the above
New in version 1.1.0: “remapping” argument
New in version 1.2.2: ability to load non-gzipped pickles; support for dill; arguments passed to loader