unpickling python 2.6 object in python 3.1.1
Question: In python 2.6, I defined a class Abc, made a dict d where keys are strings and
values are abc objects. Then I dumped this dict to a file like this-
pickle.dump(d, open('filename.pkl', 'wb'))

I can successfully load it python 2.6 with

    
    
    d1 = pickle.load(open('filename.pkl', 'rb'))
    

(d1 is identical to d)

But when I try to load it in python 3.1, only the first key (string) is loaded

    
    
    d1 = pickle.load(open('filename.pkl', 'rb'))
    
    print(d1)
    keyname1
    

The pickled file starts like

    
    
    (dp0
    S'keyname1'
    p1
    (iabc
    ABC
    p2 
    

So it seems like in 3.1, it's only loading the first string in the file. I
don't know much about encoding etc. Any ideas on what I can do to load my dict
d in 3.1.1?

Edit- my class (abc.py) is in the same working directory both times, not sure
if this matters?

Edit after answer- I realized I should have clarified better so it can be
reproduced easily. My code was buggy both because of the name clash and
because I wasn't defining my class correctly.

Anyways, I just tried the following and learnt that I should start classes
with class ABC(object): rather than just class ABC. And that I should delete
.pyc files before importing a module the second time.

What I did --

Try 1

ABC.py

    
    
    class ABC():
       pass
    

in python-2.6

    
    
    import pickle
    import ABC
    d = {'keyname1': ABC.ABC()}
    pickle.dump(d, open('filename.pkl', 'wb'))
    quit()
    

in python-3.1

    
    
    import pickle
    d1 = pickle.load(open('filename.pkl', 'rb'))
    print(d1)
     keyname1
    quit()
    

Try 2

But when I changed the class definition in ABC.py so it said class
ABC(object): instead of just class ABC:, removed the old filename.pkl, ABC.pyc
and repeated,

in python-2.6

    
    
    # same as before
    

in python-3.1

    
    
    import pickle
    d1 = pickle.load(open('filename.pkl', 'rb'))
    print(d1)
    {'keyname1': <ABC.ABC object at 0x638890>}
    


Answer: The following works fine with Python 2.6.6 and Python 3.1.3:

Python 2.6 code:

    
    
    import pickle
    
    class ABC(object):
        pass
    d = {'keyname1': ABC()}
    
    # Dump!
    pickle.dump(d, open('filename.pkl', 'wb'))
    

Python 3.1 code:

    
    
    import pickle
    
    class ABC(object):
        pass
    
    # Load:
    d1 = pickle.load(open('filename.pkl', 'rb'))
    
    print(d1)
    

The result is:

    
    
    {'keyname1': <__main__.ABC object at 0x8c99eec>}
    

So clearly loading the ABC object worked fine. I think the problem is that you
have a name clash between your module `abc` and the module called `abc` in the
standard library, and that this name clash behaves differently in Python 2 and
Python 3.

Solution: **Rename your module.**

