Pickle problem while loading a class instance.

G

gerardob

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')
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?

Thanks.

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
 
P

Peter Otten

gerardob said:
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

Peter
 
L

Lie Ryan

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?

When unpickling a user-defined class, you unpickling module must have
access to the original class definition. This means if you do this:

# model.py
class MyClass(object):
pass

# saver.py

import pickle
import model

m = model.MyClass()
pickle.dump(m, open('...', 'w'))



Then the loader.py must be able to import model. If you do not
explicitly import model, pickle will automatically try to `import model`
from the standard module search path.

# loader.py

# the import must succeed, or pickle cannot find Foo's definition
#
# import model

pickle.load(open('...'))
 
G

gerardob

I tried both things:

1- moved all the code to C:/Python26/lib/site-packages
2- Modified the PYTHONPATH in the windows registry.

However, i stil have exactly the same error on the screen.

Any other suggestions?

Thanks.


Peter said:
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

Peter
 
G

Gabriel Genellina

I tried both things:

1- moved all the code to C:/Python26/lib/site-packages
2- Modified the PYTHONPATH in the windows registry.

However, i stil have exactly the same error on the screen.

Any other suggestions?

Did you follow the advice below?

You have to re-create your pickle file, open it in binary mode 'wb'
("prueba.txt" is not a good name - it's not a text file).
Any old pickle file created in text mode won't be readable.
 
P

Peter Otten

gerardob said:
I tried both things:

1- moved all the code to C:/Python26/lib/site-packages
2- Modified the PYTHONPATH in the windows registry.

However, i stil have exactly the same error on the screen.

Any other suggestions?

Did heed my advice and make sure that your script reads and writes the
pickle file in binary mode?
file = open("prueba.txt", 'w')

The above line can trigger the same error; change "w" to "wb":
<__main__.A instance at 0x7f31d088ed88>

Seems to work. Now let's simulate the effect of writing in text and reading
in binary mode:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/pickle.py", line 1374, in loads
return Unpickler(file).load()
File "/usr/lib/python2.6/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.6/pickle.py", line 1069, in load_inst
klass = self.find_class(module, name)
File "/usr/lib/python2.6/pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named __main__

Peter
 
P

Peter Otten

gerardob said:
I tried both things:

1- moved all the code to C:/Python26/lib/site-packages
2- Modified the PYTHONPATH in the windows registry.

However, i stil have exactly the same error on the screen.

Any other suggestions?

Did you heed my advice and make sure that your script reads and writes the
pickle file in binary mode?
file = open("prueba.txt", 'w')

The above line can trigger the same error; change "w" to "wb":
<__main__.A instance at 0x7f31d088ed88>

Seems to work. Now let's simulate the effect of writing in text and reading
in binary mode:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/pickle.py", line 1374, in loads
return Unpickler(file).load()
File "/usr/lib/python2.6/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.6/pickle.py", line 1069, in load_inst
klass = self.find_class(module, name)
File "/usr/lib/python2.6/pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named __main__

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top