1

Given a python class imported in the current context, is there a way to save a serializable (picklable) reference to that class, which can be imported later on another python context?

e.g.

First save the reference. In this example get_reference and import_reference are the function I wish to implement.

import pickle
from torch.optim import SGD

sgd = SGD()
# ... do work ...

opt_reference = get_reference(sgd.__class__)
with open('SGD_ref.pkl', 'wb') as fp:
    pickle.dump(opt_reference, fp)

Then to import the reference

import pickle

with open('SGD.pkl', 'rb') as fp:
    opt_reference = pickle.load(fp)

SGD = import_reference(opt_reference)

sgd = SGD()

I do not wish to save the code or implementation of the SGD function itself, since it also imports several things on its own. I am assuming the module that I wish to save the reference of is similarly installed and imported to every python environment it is gonna be used in.

One simple approach I found is to use the function from this post to get the path for the get_reference function and then for the import reference do eval(f"import {opt_reference}"). This seems too hacky and very unsafe (although I do not care about safety in this context).

Is there any better way of doing this?

Makis Tsantekidis
  • 2,698
  • 23
  • 38
  • Do you want to save the contents of the instance or the actual class as a usable python class? – Alexander Oct 06 '19 at 13:10
  • Neither. I want to save an "importable" reference. That can be a string such as `torch.optim.SGD` but it can also be some sort of importlib spec. I am not sure what it should be, because the mechanism of importing it from the saved reference is not clear to me. – Makis Tsantekidis Oct 06 '19 at 13:15

0 Answers0