Persistence
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.
Common errors
-
ModuleNotFoundError: No module named 'Persistence'
cause The `Persistence` package is not installed in the current Python environment, or the environment is not active.fixEnsure the package is installed: `pip install Persistence`. If using a virtual environment, ensure it is activated. -
AttributeError: 'MyObject' object has no attribute '_p_oid'
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.fixEnsure 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. -
TypeError: object of type 'Persistence.Persistent' is not JSON serializable
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.fixImplement 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.
Warnings
- gotcha 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.
- breaking 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.
- breaking 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.
- deprecated 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.
Install
-
pip install Persistence
Imports
- Persistent
from persistent import Persistent
from Persistence import Persistent
Quickstart
from Persistence import Persistent
class MyPersistentObject(Persistent):
def __init__(self, value):
self.value = value
self.data = []
def add_data(self, item):
self.data.append(item)
# In a real ZODB application, this object would be managed by a Connection/Database.
# Example: (requires ZODB setup, not runnable standalone for full persistence)
# from ZODB import DB, FileStorage
# import transaction
#
# storage = FileStorage.FileStorage('my_data.fs')
# db = DB(storage)
# conn = db.open()
# root = conn.root()
#
# if 'my_obj' not in root:
# root['my_obj'] = MyPersistentObject('initial_value')
# transaction.commit()
#
# obj = root['my_obj']
# print(f'Initial value: {obj.value}, Data: {obj.data}')
# obj.add_data('new_item')
# obj.value = 'updated_value'
# transaction.commit()
#
# print(f'Updated value: {obj.value}, Data: {obj.data}')
# conn.close()
# db.close()