1

I am running the following code on VS Code to serialize a Python class:

import json
import jsonpickle
import sys


class P_C(object):
    def __init__(self, name = "default name", items = None):
        self.name = name
        self.items = items 

    def get_name (self):
        return f"Name is: {self.name}"

    def get_items (self):
        return f"Items are: {self.items}"

    def get_name_items_dict (self):
        return dict (name = self.name, items = self.items )

p_c = P_C()

with open ("P_C.json", "w") as json_file:
    json_file.write (jsonpickle.encode(p_c))

When I run this code on VS Code, I get the following error:

AttributeError: module 'jsonpickle' has no attribute 'encode'

However, when I run the exact same code on a Jupyter notebook, I get the desired output, which is inside P_C.json:

{"items": null, "name": "default name", "py/object": "__main__.P_C"}

I already checked that I am using Python 3 on VS Code

Thanks!

Diego
  • 386
  • 4
  • 19

0 Answers0