Zope Container
Zope Container (`zope.container`) is a foundational Python library within the Zope ecosystem, defining interfaces for object container components and providing concrete implementations like BTreeContainer and OrderedContainer. It serves as a building block for dynamic web applications and content management systems such as Plone, offering mechanisms for object persistence, integrity, and access control. Currently at version 7.2, the library maintains an active release cadence, frequently updating to support new Python versions and integrating changes from other Zope core packages.
Common errors
-
ImportError: No module named 'zope.container.old_module'
cause An import path for a Zope component has changed, or a compatibility import has been removed in a newer version of `zope.container` or a related Zope package.fixConsult the `zope.container` changelog or the documentation for the specific Zope package that the module belongs to. For example, `IContained` moved to `zope.location.interfaces` in older versions. -
TypeError: ('Could not adapt', <object>, <Interface>)cause This often indicates a mismatch in interface declarations or an object not properly implementing a required interface, especially after updates to `zope.interface` or related Zope packages.fixVerify that all classes intended to implement `zope.container` interfaces (like `IContainer`, `IWriteContainer`) correctly use the `@implementer` decorator from `zope.interface` and that the object itself is registered or configured to be adaptable. Ensure `zope.interface` and other Zope packages are compatible versions. -
KeyError: 'item_name' or ValueError: 'Invalid item name'
cause In modern versions (>=3.7.1), `zope.container` raises standard `KeyError` for duplication or missing keys and `ValueError` for invalid input (e.g., empty or disallowed characters in names) during item addition/retrieval.fixEnsure that keys used for adding or accessing items in a container are unique and adhere to naming conventions (e.g., non-empty, valid string characters). Handle `KeyError` for non-existent items or `ValueError` for invalid names.
Warnings
- breaking Major versions of `zope.container` (e.g., 5.0, 6.0, 7.0, 7.1) frequently drop support for older Python versions. Ensure your environment meets the `requires_python` specification (currently >=3.10 for 7.2).
- breaking Version 7.0 of `zope.container` (and other Zope packages) migrated from `pkg_resources` to PEP 420 native namespace packages. This can affect how sub-packages are discovered and imported in complex Zope deployments.
- breaking Older versions of `zope.interface` (a core dependency) had significant breaking changes in version 6.0 and 7.0, including the removal of deprecated `implements` and `classProvides` functions in favor of `@implementer` and `@provider` decorators. Although `zope.container` itself adopted these earlier (e.g., 4.0.0a1 for `implementer`), downstream code might break.
- gotcha Before `zope.container` version 3.7.1, certain container operations raised custom exceptions like `zope.exceptions.DuplicationError` or `zope.exceptions.UserError`. These were changed to standard Python exceptions `KeyError` and `ValueError` respectively.
Install
-
pip install zope-container
Imports
- BTreeContainer
from zope.container.btree import BTreeContainer
- OrderedContainer
from zope.container.ordered import OrderedContainer
- IContainer
from zope.container.interfaces import IContainer
- IWriteContainer
from zope.container.interfaces import IWriteContainer
Quickstart
from zope.container.btree import BTreeContainer
from zope.interface import implementer
from zope.container.interfaces import IContainer
@implementer(IContainer)
class MyContainer(BTreeContainer):
"""A simple container demonstrating zope.container."""
pass
container = MyContainer()
container['item1'] = 'First Value'
container['item2'] = 123
print(f"Container has {len(container)} items.")
print(f"Item 1: {container['item1']}")
# Iterating through a container
for key, value in container.items():
print(f"Key: {key}, Value: {value}")