'Pickle ModuleNotFoundError

I have a python object foo that I want to serialize so I run:

with open('foo.pkl', 'wb') as file:
    pickle.dump(foo, file)

I then submit the serialized object to a microservice in another virtual environment, now the problem is, foo depends on a module bar so when the microservice deserialize the foo.pkl file it is hit with the following error:

ModuleNotFoundError: No module named 'bar'

This makes sense, pickles require the libraries to be present when deserializing. Now the problem is, it does not make sense for me to include a copy of bar in both microservices, as this introduces a duplicate code on my code base, so my question is, is there a way that I can serialize my object while including the library bar in it so I can transfer across microservices?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source