2

I am trying to use dill to share objects between two different environments (machine learning research and production).

A simplified example:

package_a/module_a.py:

class P:
  def __init__(self, a, b):
    self.a = a #for example a dict
    self.b = b #a function

def save_A(*, path,a,b):
  import dill
  p = A(a,b)

  dill.dump(p, open(path, "wb"))

package_a/module_b.py:

a = [1,2,3]
def b():
  return 5
from module_a import save_A
save_A("test.p", a , b)

Now I am trying to load this object in a different package in a different repo package_b/prod.py:

import dill
with open("test.p",'rb') as fp:
a = dill.load(fp)

The error I am getting is:

  return StockUnpickler.find_class(self, module, name)
   ImportError: No module named 'module_a'

Dills seems to try and re-import the imports from the module where the save function is called. If I were to include the content from module_b in module_a (for example in a if __name__=="__main__" block) everything works fine.This is not feasible though, because there are a lot of objects that need to get pickled. I am using python 3.5. I don't really understand what might be causing this and why dill remembers the import paths. I could understand if i get an error complaining about A not being in the namespace (even though dill is supposed to package the class definition with the instance) but this I cannot really make sense of.

  • I don't really know anything about `dill`, but the error you get doesn't seem to be related. It looks like a simple `ImportError`, where python doesn't know about `'module_a'`. In the same package you should also have an empty `__init__.py` file. Read more about the __init__ file [here](https://stackoverflow.com/questions/448271/what-is-init-py-for) – Adelin Sep 11 '17 at 14:36
  • The example is simplified. When I try to import the pickle file in package_a everything works, so the code is not broken – Valentin Zambelli Sep 11 '17 at 14:42

0 Answers0