Pickle problem while loading a class instance.

Peter Otten __peter__ at web.de
Tue Apr 6 14:02:59 EDT 2010

gerardob wrote:

> Hello, I am new to python and i have a problem using the pickle load
> function.
> I have an object m of the class MarkovModel and i want to copy it to a
> file and load it onto another class:
> l=[1,2,3]
> m = markov_model.MarkovModel()
> m.load_observations(l)
> file = open("prueba.txt", 'w')

Remember to open the file in binary mode.

> pickle.dump(m,file,2)
> file.close()
> #m2 = markov_model.MarkovModel()
> file = open("prueba.txt", 'rb')
> m2 = pickle.load(file) (THIS IS LINE 36)
> The error below appears. In the case i remove the comment to initialize
> m2, the same thing happens. Any ideas on how to fix this?

Add the directory containing the markov_model module to your PYTHONPATH 
environment variable or move the module into a directory where Python is 
already looking (C:/Python26/lib/site-packages or the per-user equivalent).

See also http://docs.python.org/using/windows.html#finding-modules

> Traceback (most recent call last):
> File "C:\Users\gberbeglia\Documents\python\scripting\mycodes\main.py",
> line 36, in <module>
> m2 = pickle.load(file)
> File "C:\Python26\lib\pickle.py", line 1370, in load
> return Unpickler(file).load()
> File "C:\Python26\lib\pickle.py", line 858, in load
> dispatch[key](self)
> File "C:\Python26\lib\pickle.py", line 1090, in load_global
> klass = self.find_class(module, name)
> File "C:\Python26\lib\pickle.py", line 1124, in find_class
> __import__(module)
> ImportError: No module named markov_model


More information about the Python-list mailing list