Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cPickle - module object has no attribute #49759

Closed
ntroutman mannequin opened this issue Mar 18, 2009 · 5 comments
Closed

cPickle - module object has no attribute #49759

ntroutman mannequin opened this issue Mar 18, 2009 · 5 comments

Comments

@ntroutman
Copy link
Mannequin

ntroutman mannequin commented Mar 18, 2009

BPO 5509
Nosy @benjaminp, @basak
Files
  • Foo.py: Creates a list of Foo objects and pickles them
  • LoadFoo.py: Loads the pickled list
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-03-18.19:08:30.441>
    created_at = <Date 2009-03-18.17:08:11.311>
    labels = []
    title = 'cPickle - module object has no attribute'
    updated_at = <Date 2009-12-05.18:51:50.355>
    user = 'https://bugs.python.org/ntroutman'

    bugs.python.org fields:

    activity = <Date 2009-12-05.18:51:50.355>
    actor = 'rb'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-03-18.19:08:30.441>
    closer = 'benjamin.peterson'
    components = ['None']
    creation = <Date 2009-03-18.17:08:11.311>
    creator = 'ntroutman'
    dependencies = []
    files = ['13364', '13365']
    hgrepos = []
    issue_num = 5509
    keywords = []
    message_count = 5.0
    messages = ['83759', '83760', '83766', '83770', '95998']
    nosy_count = 3.0
    nosy_names = ['benjamin.peterson', 'rb', 'ntroutman']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue5509'
    versions = ['Python 2.5']

    @ntroutman
    Copy link
    Mannequin Author

    ntroutman mannequin commented Mar 18, 2009

    If I define a class Foo in module A and in module A pickle out a list of
    Foo objects to 'foo.pkl', then in module B attempt to unpickle 'foo.pkl'
    I recieve the error "AttributeError: 'module' object has no attribute 'Foo'"

    Attached are:
    Foo.py which defines the class Foo and pickles out a list of objects

    LoadFoo.py attempts to load the list of objects pickled by Foo

    I'm running Vista with "Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08)
    [MSC v.1310 32 bit (Intel)] on win32"

    @ntroutman
    Copy link
    Mannequin Author

    ntroutman mannequin commented Mar 18, 2009

    Why can I only attach one file at a time?

    @ntroutman
    Copy link
    Mannequin Author

    ntroutman mannequin commented Mar 18, 2009

    I believe I've tracked down the problem. When you run a python module
    directly (ie "python Foo.py") any classes defined in the module have
    their '__module__' attribute set to '__main__'. Which means the pickle
    says the class is in '__main__' of whatever module is trying to load the
    file.

    I think it would make more sense to actually include the module name,
    this means that an external module need simply ensure that the pickled
    class's module be imported with the correct name.

    @benjaminp
    Copy link
    Contributor

    Implementing this may be difficult (the module may not be on the search
    path when its run and there's no way to determine that). Also, it would
    break compatibility. Anyway, it's usually better to define classes that
    will be pickled in their own permanent module.

    @basak
    Copy link
    Mannequin

    basak mannequin commented Dec 5, 2009

    I've just been stung by this. I've noticed that this seems to apply to
    both cPickle and pickle. Even worse, it causes different behaviour when
    a program is run under pdb because __main__ is suddenly pdb rather than
    the program itself.

    So, in summary, neither pickle nor cPickle can pickle a class if it is
    not defined in its own module? Surely this is an obvious deficiency of
    pickle and if it is not going to be fixed it should at least be
    documented as such?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant