Use a pickled custom-class in another file without importing the class first

Viewed 34

Say I have file creator.py with the following code

class Mapper:
    def train(self, X,y):
         return self
    def predict(self, X):
        pass

I want to distribute this object, e.g in a google-cloud storage, such that my co-workes have access to that class aswell.

I thought it would be as easy as just using pickle to save a Mapper instance e.g

import pickle


mapper = Mapper()
mapper.train(X,y)

with open("my_mapper.pkl","wb") as f:
    pickle.dump(f,mapper)

and then in another script, in another repo, main.py I can un-pickle it

import pickle
download_model_from_storage("my_mapper.pkl") # Download the object from storage
with open("my_mapper.pkl","rb") as f:
    my_mapper = pickle.load(f)

but that throws the error

AttributeError: Can't get attribute 'Mapper' on <module '__main__' from 'c:\\code\\main.py'>.

The only way I can fix this is to first import Mapper in main.py, but that introduces a file that I'll need to track in case I want to do some changes in creator.py then I should make sure those changes are also in main.py (which is rather prone to errors).

Isn't there a way to just distribute custom classes without having access to the "mother-class"? And why not (if that's the case)

0 Answers
Related