2

I have an object:

class X():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

whose attribute c, is a of list objects (of a different kind):

class Y():
    def __init__(self, x, y):
        self.x = x
        self.y = y

I pickle this as follows:

import pickle
pickle.dump(instance_of_class_X,open(dir, "wb"))

I load as follows:

import pickle
from some_library import X, Y # I import the two classes involved
pickle.load(open(dir,"rb"))

I get the following error:

AttributeError: 'module' object has no attribute 'Y'

Not sure what to do, any help would be highly appreciated.

1
  • 1
    where is the exception being raised? Commented Jun 10, 2012 at 19:40

1 Answer 1

4

Possibly you are falling victim to the need of pickle to have the class available via the same fully-qualified name as was originally used. It would depend on what namespace X and Y are in when you originally create the object. See this answer and this page.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you are right. I had class X on the python file I ran, and class Y was inside of a function that I imported from a library (I did this for organization purposes). I copied and pasted the function I was importing to the python file I was running, and then pulled the class (Y) out of the function so that it was just like my other class X. Now it works. My code is a lot messier, but now it works. Wasn't aware of this subtlety in Python. Thanks Mate :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.