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?