Search code examples
pythonpython-2.7joblib

Joblib ImportError. Tries to load a library I think it shouldn't be looking for in the first place


I am saving an object with joblib.dump(). When I try to open it using another Python instance (but same version), joblib complains it can't load a particular module:

ImportError: No module named some_module

Now, this module some_module is indeed not available in that Python instance. The point is, however, that the object I was trying to load does not need that module at all.

So my question is, why does joblib think it needs this package?

Does it somehow include all module that were active at the time of dumping?


Solution

  • Joblib uses pickle. The pickle can store any arbitrary Python object to disk and restore it into another process afterwards. But if that Python object is, or contains, an instance of a class that is defined in the code that does the dump, then that class definition needs to be available in the code that does the load.

    And if that class is defined in a library that was imported by the code that does the dump, then it also needs to import that library at load time. You don't have to do the import: pickle will do that for you. But it must be available for import.

    I understand that you don't think that the object you are trying to load needs the class. But pickle does think that.