1

I imported pickle from copyreg. I then use the attribute dumps to convert a dictionary into bytes b'\x80\x04...' but it results in the error shown below:

from copyreg import pickle
d={"name":value}
d=pickle.dumps(d)

Error:

AttributeError: 'function' object has no attribute
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Kobina Adu
  • 27
  • 4
  • From the copyreg docs, https://docs.python.org/3/library/copyreg.html#copyreg.pickle, that's a function used to register custom picklers. That's a poor name for a function if you ask me! Yours is an easy mistake to make. – tdelaney Feb 12 '22 at 18:03
  • 1
    This is a "typo question". It shouldn't be a question at all. Not saying that you shouldn't have asked it, but knowing now that it was just a question of a trivial mistake, the best thing for SO would be for you to delete this question to avoid cruft in the machinery. This isn't a useful question to have survive. Just my opinion, but I expect that many would agree. – CryptoFool Feb 12 '22 at 18:09
  • "I imported pickle from copyreg" Why? That's not the ``pickle`` module. – MisterMiyagi Feb 12 '22 at 18:18
  • @CryptoFool - I think this one is legitimate. `copyreg` is used in pickling and it has a "pickle" variable which one can easily assume is the pickle module. Seems like an easy mistake to make... and that makes it a good question for stackoverflow. Others have the same problem, see this question, and get a solution. – tdelaney Feb 12 '22 at 18:35
  • @tdelaney - the problem with your logic is that this sort of mistake happens all the time, even to advanced programmers. Here, the error message is explaining what's wrong. If we had to go to SO every time we made such a trivial mistake, we'd work at a snail's pace. If a goal of SO was to explain how to fix all such "easy mistakes", SO would be 1000 times the size it is now and likely 1000 times less useful in us all having to wade through all the crud. I'm not saying that this isn't a valid question. Rather, it's just not a valuable one in terms of what SO is trying to accomplish. – CryptoFool Feb 12 '22 at 18:44
  • @tdelaney I'm a bit stumped how one would arrive in that situation. ``copyreg.pickle`` is practically *the* thing why one would use ``copyreg`` to begin with, it's pretty clearly some function and not the ``pickle`` module. – MisterMiyagi Feb 12 '22 at 18:56
  • @MisterMiyagi - Someone new to pickling might see examples of `pickle` and `copyreg`, which is part of the pickle ecosystem. its actually pretty reasonable to think that something called "pickle" in a pickle module like `copyreg` is the right thing to use. Its a poorly named function. The name should include some hint that you are registering things. It shouldn't just be something as generic and misleading as "pickle". So, poorly named function results in misunderstanding. That's understandable. And good for stackoverflow to clear it up. – tdelaney Feb 12 '22 at 19:45
  • @tdelaney you are right. I started programming so few months a go. I tasked myself with a project and I got to a part where I wanted to save a dictionary into SQLite DB but it wouldn't allow me. I called a friend who it a programmer and he asked me to use pickle to convert it into a bytes and then store it. It was my first time using the module `pickle`. Thank you all. – Kobina Adu Feb 13 '22 at 22:51

2 Answers2

2

Your import seems incorrect. It should be:

from pickle import dumps

d = {..}
d = dumps(d)
Jay Patel
  • 53
  • 4
0

I imported from the wrong library. The code should have been:

import pickle
d={...}
d=pickle.dumps(d)
Kobina Adu
  • 27
  • 4