1

so I created a book class and added its instances to a list that I'm trying to pickle but I'm getting a strange error. Help appreciated!

with open("book_data/books.pkl", "wb") as f:
        pickle.dump(book_list, f)

Output:

PicklingError                             Traceback (most recent call last)
<ipython-input-24-45ad0cec62df> in <module>
      1 with open("book_data/books.pkl", "wb") as f:
      2 
----> 3         pickle.dump(book_list, f)

PicklingError: Can't pickle <class '__main__.book'>: it's not the same object as __main__.book
  • @will_f the books are instances of the book class, `class book: def __init__(self,title,author,genre,description,rating,nbratings,nbpages): self.title=title self.author=author self.genre=genre self.description=description self.rating=rating self.nbratings=nbratings self.nbpages=nbpages ` here is an example instance: `b=book(t,a,g,d,r,nbr,nbp)` `book_list.append(b)`, btw I did the same thing using pycharm and it worked just fine. It seems that the issue is exclusive to Jupyter... – Adib Menchali May 29 '20 at 01:44
  • [this](https://stackoverflow.com/a/50985430/11305111) might help – jayveesea May 29 '20 at 10:53
  • @jayveesea that's pretty handy but it gave me a recursion error when I tried it, `RecursionError Traceback (most recent call last) in 1 import dill ----> 2 dill.dump_session('Data_Gathering.db')` – Adib Menchali May 29 '20 at 17:24
  • [`%store`](https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html) is another solution which I have used in the past. `%store someVar` and then `%store -r` to retrieve. – jayveesea May 30 '20 at 11:37

0 Answers0