{"id":9175,"library":"persistence","title":"Persistence","description":"The `Persistence` package (version 5.4) provides a specific variant of the persistent base class, `Persistent`, implemented as an ExtensionClass. It is part of the Zope Foundation ecosystem and is primarily intended for use when ExtensionClass semantics are required. For general-purpose object persistence without these specific semantics, the `persistent.Persistent` class from the `persistent` distribution is typically recommended. The library is mature and receives occasional updates, with its most recent release in November 2025.","status":"active","version":"5.4","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/Persistence","tags":["persistence","zope","zodb","extensionclass","database","object database"],"install":[{"cmd":"pip install Persistence","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The `persistent` library provides a more general `Persistent` class. This `Persistence` library provides an ExtensionClass variant. Use the `persistent` library if ExtensionClass semantics are not needed.","wrong":"from persistent import Persistent","symbol":"Persistent","correct":"from Persistence import Persistent"}],"quickstart":{"code":"from Persistence import Persistent\n\nclass MyPersistentObject(Persistent):\n    def __init__(self, value):\n        self.value = value\n        self.data = []\n\n    def add_data(self, item):\n        self.data.append(item)\n\n# In a real ZODB application, this object would be managed by a Connection/Database.\n# Example: (requires ZODB setup, not runnable standalone for full persistence)\n# from ZODB import DB, FileStorage\n# import transaction\n#\n# storage = FileStorage.FileStorage('my_data.fs')\n# db = DB(storage)\n# conn = db.open()\n# root = conn.root()\n#\n# if 'my_obj' not in root:\n#     root['my_obj'] = MyPersistentObject('initial_value')\n#     transaction.commit()\n#\n# obj = root['my_obj']\n# print(f'Initial value: {obj.value}, Data: {obj.data}')\n# obj.add_data('new_item')\n# obj.value = 'updated_value'\n# transaction.commit()\n#\n# print(f'Updated value: {obj.value}, Data: {obj.data}')\n# conn.close()\n# db.close()","lang":"python","description":"Defines a basic persistent class using `Persistence.Persistent`. Note that for true persistence, this class needs to be instantiated and managed within a ZODB (Zope Object Database) context, which typically involves setting up a storage and connection."},"warnings":[{"fix":"Evaluate your project's needs. If ExtensionClass is not a strict requirement, consider `pip install persistent` and `from persistent import Persistent` instead.","message":"The `Persistence` library provides a `Persistent` class that is an ExtensionClass. If you do not specifically require ExtensionClass semantics, it is recommended to use `persistent.Persistent` from the `persistent` distribution, which is the more common and general-purpose persistence base class.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your ZODB installation is version 3.11 or newer if you intend to use it with this standalone `Persistence` library. If using an older ZODB, rely on its bundled persistence mechanisms.","message":"This standalone `Persistence` release (and its related `persistent` package) is not recommended or supported with ZODB versions older than 3.11. ZODB 3.10 and earlier versions bundled their own internal version of the `persistent` package, which can lead to conflicts.","severity":"breaking","affected_versions":"< 5.0 (for standalone use with ZODB < 3.11)"},{"fix":"Upgrade your Python environment to 3.10 or newer (Persistence 5.4 requires >=3.10) to use `Persistence` version 5.0 or later.","message":"Version 5.0 of `Persistence` (and the closely related `persistent` library) dropped support for older Python versions, specifically Python 2.7, 3.5, and 3.6.","severity":"breaking","affected_versions":"< 5.0"},{"fix":"Avoid using `persistent.cPersistence.simple_new`. If migrating from older code, review its usage and replace it with appropriate object instantiation patterns.","message":"The function `persistent.cPersistence.simple_new` was removed in version 6.1 of the `persistent` library. While this is specifically for `persistent`, users migrating or confusing the two packages might encounter this if they relied on this function with older `persistent` versions.","severity":"deprecated","affected_versions":"persistent >= 6.1"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed: `pip install Persistence`. If using a virtual environment, ensure it is activated.","cause":"The `Persistence` package is not installed in the current Python environment, or the environment is not active.","error":"ModuleNotFoundError: No module named 'Persistence'"},{"fix":"Ensure objects inheriting from `Persistence.Persistent` are instantiated and operated upon within the context of an active ZODB database connection and managed by a ZODB Storage/Connection.","cause":"This error typically occurs when an object intended to be persistent (inheriting from `Persistence.Persistent`) is instantiated or used outside of a properly initialized ZODB connection, or its persistence state is not correctly managed.","error":"AttributeError: 'MyObject' object has no attribute '_p_oid'"},{"fix":"Implement custom JSON serialization for your persistent objects. This usually involves defining a `__json__` method or providing a custom `json.JSONEncoder` that knows how to convert your persistent object into a JSON-serializable dictionary or other basic types.","cause":"Instances of `Persistence.Persistent` (or any custom class inheriting from it) are not natively JSON serializable. Attempting to directly serialize these objects using `json.dumps()` will fail.","error":"TypeError: object of type 'Persistence.Persistent' is not JSON serializable"}]}