Search code examples
pythonpickletopic-modeling

Pickle AttributeError: Can't get attribute 'Wishart' on <module '__main__' from 'app.py'>


I already run my code to load my variable saved by pickle. This my code

import pickle 
last_priors_file = open('simpanan/priors', 'rb') 
priors = pickle.load(last_priors_file)

and i get error like this : AttributeError: Can't get attribute 'Wishart' on <module '__main__' from 'app.py'>


Solution

  • This happens because the pickled data was saved when the script is run as __name__ == '__main__' and so it saves the location of Wishart as __main__.Wishart. Then, when you run your next script to load the data, there is no Wishart in scope so it fails.

    The fix in this case is to simply add from wherever import Wishart before the call to pickle.load.